原数据:
const list = [
{id:1, key: 'a'},
{id:1, key: 'b'},
{id:2, key: 'c'},
{id:2, key: 'd'},
]
期望数据格式
const newList = [
{id:1, keyList: ['a','b']},
{id:2, keyList: ['c','d']},
]
let newList=
_(list )
.flatten()
.groupBy('id')
.map(
_.spread((...values) => {
console.log(values, 'values');
let keyList: string[] = [];
let newId = '';
values?.forEach((item: any, index: number) => {
if (index === 0) {
newId = item?.id;
}
keyList.push(item?.key);
});
return { id: newId , keyList};
}),
)
.value() || [];
console.log(newList, 'newList');
页面输出: