将以下数组中no字段值相同的其他字段值去重合并用顿号隔开,字段d默认第一个
let arr = [
{no:"001",spec:"1*1",model:"模型1",d:"111"},
{no:"001",spec:"1*2",model:"模型2",d:"222"},
{no:"001",spec:"1*3",model:"模型1",d:"111"},
{no:"002",spec:"2*2",model:"模型1",d:"222"},
];
let mergedArr = arr.reduce((acc, curr) => {
debugger;
let existingElement = acc.find(el => el.no === curr.no);
if (existingElement) {
if(existingElement.spec.indexOf(curr.spec)<0){
existingElement.spec += `、${curr.spec}`;
}
if(existingElement.model.indexOf(curr.model)<0){
existingElement.model += `、${curr.model}`;
}
} else {
acc.push(curr);
}
return acc;
}, []);
console.log(mergedArr);