ts 数组对象根据某个字段分组
代码
ts 数组对象根据某个字段分组
let arr = [{group:"第一组",value:6},{group:"第一组",value:3},{group:"第一组",value:4},{group:"第二组",value:9},{group:"第二组",value:7}]
const res = arr.reduce((result, item) => {
let index = result.findIndex((it) => it.group == item.group);
if (index == -1) {
result.push({
group: item.group,
children: [item]
});
} else {
result[index].children.push(item);
}
return res
共有 0 条评论