方法:
// 将相同name的进行分组
for (let i = 0; i < newArr1.length; i++) {//newArr1为原始json数据
// 利用some函数来区分是否相同
if (
!obj.some(item => item.name === newArr1[i].name &&
item.width === newArr1[i].width &&
item.height === newArr1[i].height &&
item.widthDeviation === newArr1[i].widthDeviation &&
item.heightDeviation === newArr1[i].heightDeviation &&
item.tempered === newArr1[i].tempered &&
item.floorNum === newArr1[i].floorNum &&
item.processRequirements === newArr1[i].processRequirements &&
item.remark === newArr1[i].remark
)) {
obj.push(newArr1[i])
arr.push({
gname: newArr1[i].name,
gwidth: newArr1[i].width,
gheight: newArr1[i].height,
gwidthDeviation: newArr1[i].widthDeviation,
gheightDeviation: newArr1[i].heightDeviation,
gtempered: newArr1[i].tempered,
gfloorNum: newArr1[i].floorNum,
gprocessRequirements: newArr1[i].processRequirements,
gremark: newArr1[i].remark,
data: [newArr1[i]]
})
} else {
// 把那些和键值相同的条目放入分组(玻璃名)
for (let k = 0; k < arr.length; k++) {
if (arr[k].gname === newArr1[i].name &&
arr[k].gwidth === newArr1[i].width &&
arr[k].gheight === newArr1[i].height &&
arr[k].gwidthDeviation === newArr1[i].widthDeviation &&
arr[k].gheightDeviation === newArr1[i].heightDeviation &&
arr[k].gtempered === newArr1[i].tempered &&
arr[k].gfloorNum === newArr1[i].floorNum &&
arr[k].gprocessRequirements === newArr1[i].processRequirements &&
arr[k].gremark === newArr1[i].remark
) {
arr[k].data.push(newArr1[i])
}
}
}
}
const arr2 = []
var newArr1s = []
// 循环分组数据即可,然后针对特定项目做处理
// 注意点在于三元表达式,因为数组必须有两个才能用reduce函数(缩减函数)
arr.forEach(item => {
console.log(`j88888---`, item)
arr2.push({
// name: item.data.map(item => item.name).join(','),
name: item.gname,
width: item.gwidth,
height: item.gheight,
widthDeviation: item.gwidthDeviation,
heightDeviation: item.gheightDeviation,
number: item.data.length > 1 ? item.data.map(item => item.number).reduce((pre, next) => pre + next) : item.data[0].number,//此处为相加方法
price: item.data[0].price,
tempered: item.gtempered,
floorNum: item.gfloorNum ? item.gfloorNum : item.data.map(item => item.floorNum).join(','),
processRequirements: item.gprocessRequirements ? item.gprocessRequirements : item.data.map(item => item.processRequirements).join(','),//此处为字符串拼接方法
remark: item.gremark ? item.gremark : item.data.map(item => item.remark).join(',')
// tempered: this.sss,
// name: item.data[0].name
})
})
来看对比图:
合并前:
合并后:
需要的可直接贴代码。