1 回答
TA贡献1966条经验 获得超4个赞
您只需要注意一些小改动,请使用以下几行更新您的代码:
import { Component, OnInit } from "@angular/core";
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent implements OnInit {
groceryList = [];
groups = [
{ grpId: 1, grpName: "Vegetables" },
{ grpId: 2, grpName: "Fruits" },
{ grpId: 3, grpName: "Staples" }
];
subGrp = [
{ subGrpId: "V1", grpId: 1, subGrpName: "Carrot" },
{ subGrpId: "F1", grpId: 2, subGrpName: "Apple" },
{ subGrpId: "S1", grpId: 3, subGrpName: "Dal" }
];
quantity = [
{ quantityValue: "1kg", subGrpId: "V1" },
{ quantityValue: "2kg", subGrpId: "V1" },
{ quantityValue: "1kg", subGrpId: "S1" }
];
ngOnInit() {
this.groceryList = [];
const grpSet = new Set(
this.groups.map(i => ({ id: i.grpId, name: i.grpName }))
);
grpSet.forEach(g =>{
let element:any[];
element= this.subGrp.filter(el => {
if (el.grpId === g["id"] && this.quantity) {
el["quantities"] = this.quantity.filter(
v => v.subGrpId === el["subGrpId"]
);
if (el["quantities"].length > 0) {
return el;
}
}
})
if(element && element.length){
this.groceryList.push({
grp:g["name"],
grpTypeList:element
})
}
}
);
console.log(JSON.stringify(this.groceryList));
}
}
添加回答
举报