groupBy

需要分组的数组 

 //需要分组的数组   
var menuList=[{ 
      "name":'a',
    "activeMenu": "11111", 
},{ 
      "name":'b',
    "activeMenu": "11111", 
},{ 
      "name":'c',
    "activeMenu": "2", 
}]

groupBy 

 
        const groupBy = (list, fn) => {
            const groups = {};
            list.forEach(function (o) {
                const group = JSON.stringify(fn(o));
                groups[group] = groups[group] || [];
                groups[group].push(o);
            });
            return groups;
        }; 

使用 

       var arr = groupBy(menuList, link => {
    //根据activeMenu
            return link.activeMenu;
        });

 结果

    "1111":[
                { activeMenu: "1111", name: "a"  },
                { activeMenu: "1111", name: "b"  }, 
           ]  ,
    "2":[
                { activeMenu: "2", name: "c"  }, 
           ]   

你可能感兴趣的:(javascript,前端,开发语言)