Skip to content

Commit 631d3b6

Browse files
authored
Merge pull request #19 from SanjayBoricha/add-get-only-function
Added get_only function to get array of only selected keys
2 parents 6e3b72b + c2d36a4 commit 631d3b6

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,23 @@ function sanitize_array(arr,schema){
277277
return arr
278278
}
279279

280+
/**
281+
* Get only selected keys of array
282+
* Like select query
283+
*
284+
* @param array arr
285+
* @param array keys
286+
* @returns array
287+
*/
288+
function get_only(arr, keys) {
289+
is_array(arr);
290+
is_array(keys);
291+
return arr.map((a) => keys.reduce((b, key) => {
292+
b[key] = a[key];
293+
return b;
294+
}, {}))
295+
}
296+
280297
export {
281298
is_array,
282299
is_num_array,
@@ -297,4 +314,5 @@ export {
297314
intersection,
298315
difference,
299316
sanitize_array,
317+
get_only,
300318
};

0 commit comments

Comments
 (0)