groupBy
2021-10-08 本文已影响0人
泠泉
var groupBy = (arr, key) => {
return arr.reduce((acc, e)=>{
(acc[e[key]] = acc[e[key]] || []).push(e);
return acc;
}, {});
}
console.log(groupBy(['one', 'two', 'three'], 'length'));
// => {3: ["one", "two"], 5: ["three"]}