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"]}
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"]}
本文标题:groupBy
本文链接:https://www.haomeiwen.com/subject/abiqoltx.html
网友评论