groupBy

作者: 泠泉 | 来源:发表于2021-10-08 16:37 被阅读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"]}
    

    相关文章

      网友评论

          本文标题:groupBy

          本文链接:https://www.haomeiwen.com/subject/abiqoltx.html