mirror of
https://github.com/fmhy/edit.git
synced 2026-02-18 01:01:23 +11:00
improve
This commit is contained in:
parent
d9db74cb59
commit
2d0c2cdd47
1 changed files with 9 additions and 1 deletions
|
|
@ -1,3 +1,11 @@
|
|||
const arr = [{name: 'Alice', age: 30}, {name: 'Bob', age: 25}, {name: 'Charlie', age: 30}];
|
||||
const groupedByAge = groupBy(arr, item => item.age);
|
||||
|
||||
const groupedByAge = arr.reduce((groups, item) => {
|
||||
if (!groups[item.age]) {
|
||||
groups[item.age] = [];
|
||||
}
|
||||
groups[item.age].push(item);
|
||||
return groups;
|
||||
}, {});
|
||||
|
||||
// groupedByAge is now equal to {25: [{name: 'Bob', age: 25}], 30: [{name: 'Alice', age: 30}, {name: 'Charlie', age: 30}]}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue