对JS数组进行归类(根据id组成新数组)
两个数组更具id组成新的数组。
A数组的Id,和B数组的TypeId。如何一样的话,就组成新的数组。
this.shopListDatas是上面的A数组
this.shopCateGoryData是上面的B数组
A数组
{
"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数组
{
"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;
},