1:根据某个相同属性的来组成数据,不影响原数组条数
//数据
"data": {
"records": [
{
"spuCode": "1000000032538",
"spuName": "小杨眼镜",
"skuCode": "1000000035809",
"skuName": "小杨眼镜",
"shopCode": "GMJ-MS-CGCTG-B1G",
"shopName": "小杨零售店",
},
{
"spuCode": "1000000032538",
"spuName": "小杨眼镜",
"skuCode": "1000000035810",
"skuName": "小杨眼镜",
},
{
"spuCode": "1000000032536",
"spuName": "不能现场拿走",
"skuCode": "1000000035807",
"skuName": "不能现场拿走",
}
]
}
combine() {
const map = {};
const arr = this.data.records;
for (let i = 0; i < arr.length; i++) {
const ai = arr[i];
if (!map[ai.spuCode]) {
map[ai.spuCode] = [ai];
} else {
map[ai.spuCode].push(ai);
}
}
const res = [];
Object.keys(map).forEach(key => {
res.push({
spuCode: key,
data: map[key]
});
});
console.log(res);
}