Arrays *按项目数量列出的循环
我有项目数组Arrays *按项目数量列出的循环,arrays,angular,angular8,Arrays,Angular,Angular8,我有项目数组 [ { "Name": "Bread", "Price": 10, "quantity": 4 }, { "Name": "Jam", "Price": 2, "quantity": 0 }, { "Name": "Butter", "Price": 5, "quantity": 1 }, ... ] *ngFor=“let item of items”循环项目一次,但我希望它按数量循环每个
[
{
"Name": "Bread",
"Price": 10,
"quantity": 4
},
{
"Name": "Jam",
"Price": 2,
"quantity": 0
},
{
"Name": "Butter",
"Price": 5,
"quantity": 1
},
...
]
*ngFor=“let item of items”循环项目一次,但我希望它按数量循环每个项目,也就是说,“面包”应该循环4次,而果酱不应该出现在循环中。在javascript中创建新数组更好、更简单:
items = [ ... ];
itemQuantities = [];
ngOnInit(): void {
this.items.forEach(item => {
for (let i = 0; i < item.quantity; i++) {
itemQuantities.push(item);
}
});
}
items=[…];
项目数量=[];
ngOnInit():void{
this.items.forEach(item=>{
for(设i=0;i
您尝试了什么,到底有什么问题?我已经更新了我的答案,以说明您的问题所在。