Classify the JS array (form a new array according to the id).
The two arrays have more ids to form a new array.
The Id of the A array, and the TypeId of the B array. If it is the same, a new array is formed.
this.shopListDatas is the A array above
this.shopCateGoryData is the B array above
A array
{
"code": 1,
"msg": "获取成功",
"list": [
{
"Id": 6,
"Name": "印务服务",
"CreateTime": "/Date(1685080107500)/",
"Sort": 1
},
{
"Id": 1,
"Name": "寄存服务",
"CreateTime": "/Date(1684722681063)/",
"Sort": 2
},
{
"Id": 2,
"Name": "售卖服务",
"CreateTime": "/Date(1684722681070)/",
"Sort": 3
},
{
"Id": 7,
"Name": "6776",
"CreateTime": "/Date(1685928382607)/",
"Sort": 6
}
]
}
B array
{
"code": 1,
"msg": "获取成功",
"list": [
{
"Id": 8,
"TopImg": "http://zyt.helpexpo.cn/",
"Name": "A4黑白打印",
"Price": 3.00,
"Unit": "页",
"Describe": "<p>A4黑白打印</p>",
"Specifications": [],
"TypeId": 1
},
{
"Id": 9,
"TopImg": "http://zyt.helpexpo.cn/",
"Name": "A3黑白打印",
"Price": 6.00,
"Unit": "页",
"Describe": "<p>A3黑白打印</p>",
"Specifications": [],
"TypeId": 6
},
{
"Id": 17,
"TopImg": "http://zyt.helpexpo.cn/",
"Name": "A3彩打",
"Price": 10.00,
"Unit": "页",
"Describe": "<p>A3彩打</p>",
"Specifications": [],
"TypeId": 7
}
]
}
this.shopListDatas是上面的A数组
this.shopCateGoryData是上面的B数组
mapArray() {
const groupedProducts = this.shopCateGoryData.map(type => {
const typeId = type.Id;
const productList = this.shopListData.filter(product => product.TypeId ===
typeId).map(product => ({
id: product.Id,
name: product.Name,
img: product.TopImg,
unit: product.Unit,
describe: product.Describe,
specifications: product.Specifications
}));
return {
id: typeId,
name: type.Name,
sort: type.Sort,
createTime: type.CreateTime,
productList: productList
};
});
this.productClass = groupedProducts;
},