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"]}
上一篇下一篇

猜你喜欢

热点阅读