js array group by
作者:
唐植超 | 来源:发表于
2019-12-13 19:04 被阅读0次function groupBy(array, f) {
let groups = [];
let indexList = [];
//给每一个项增加一个组名
let dataList = array.map(item =>{
item.groupName = f(item);
return item;
});
//排除重名
indexList = Array.from(new Set(dataList.map(item => item.groupName))) ;
//根据组名分组
indexList.forEach(item =>{
groups.push({
word : item ,
list: dataList.filter(sitem => {
return sitem.groupName === item;
})
})
});
return groups;
}
本文标题:js array group by
本文链接:https://www.haomeiwen.com/subject/wlkqnctx.html
网友评论