Skip to content

Commit 562b745

Browse files
committed
Added get_only function to get array of only selected keys
1 parent 0353d83 commit 562b745

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
@@ -179,6 +179,23 @@ function get_element_by_key_value(arr, key, value) {
179179
return arr.filter((item) => item[key] == value);
180180
}
181181

182+
/**
183+
* Get only selected keys of array
184+
* Like select query
185+
*
186+
* @param array arr
187+
* @param array keys
188+
* @returns array
189+
*/
190+
function get_only(arr, keys) {
191+
is_array(arr);
192+
is_array(keys);
193+
return arr.map((a) => keys.reduce((b, key) => {
194+
b[key] = a[key];
195+
return b;
196+
}, {}))
197+
}
198+
182199
export {
183200
is_array,
184201
is_num_array,
@@ -195,4 +212,5 @@ export {
195212
array_slice_sum,
196213
sort_nums,
197214
get_element_by_key_value,
215+
get_only,
198216
};

0 commit comments

Comments
 (0)