Skip to content

Commit 6d20dcb

Browse files
authored
Merge branch 'main' into add-for-in-range-func
2 parents c6d5349 + 99cbfeb commit 6d20dcb

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,21 @@ function for_in_range(num, callback) {
497497
}
498498
}
499499

500+
/**
501+
* Group by key in array of object
502+
*
503+
* @param array arr
504+
* @param string key
505+
* @returns object
506+
*/
507+
function group_by(arr, key) {
508+
is_array(arr)
509+
return arr.reduce(function (rv, x) {
510+
(rv[x[key]] = rv[x[key]] || []).push(x);
511+
return rv;
512+
}, {});
513+
}
514+
500515
export {
501516
is_array,
502517
is_num_array,
@@ -525,4 +540,5 @@ export {
525540
find_key_and_update,
526541
find_and_update,
527542
for_in_range,
543+
group_by,
528544
};

0 commit comments

Comments
 (0)