angular - for循环中的离子4多阵列
问题描述
我需要*ngFor
在ionic4中使用一个。
这是我的代码,
<ion-item *ngFor="let item of data.name">
<ion-checkbox mode="md" [(ngModel)]="data.checked" value="0" ></ion-checkbox>
<ion-label class="title-add">{{item}} </ion-label>
<ion-label slot="end" class="ion-text-end price-add">
<ion-text color="medium">+ {{prop}} ريال </ion-text>
</ion-label>
</ion-item>
我有两个数组
data.name
和
data.price
我需要在 ion-item 中创建相同的循环。我怎样才能做到?
API 响应:
male_addition: [
{
id: 33,
name: [
"pepsi",
"cola"
],
name_additions: "drinks",
price: [
"3",
"3"
],
type: 2,
male_id: 15,
details_id: 45,
created_at: "2020-02-28 16:16:18",
updated_at: "2020-02-28 16:16:18"
}
]
解决方案
您应该将两个数组映射到一个新的类型为 的对象数组中,假设它们总是相互关联{name: string; price: number;}
。
做这样的事情,
this.namePriceArray = this.male_addition.flatMap(data =>
data.name.map((n, i) => ({name: n, price: data.price[i]})));
然后你就可以this.namePriceArray
在你的*ngFor
里面使用了<ion-item>
。
<ion-item *ngFor="let item of namePriceArray">
<ion-checkbox mode="md" [(ngModel)]="data.checked" value="0" ></ion-checkbox>
<ion-label class="title-add">{{item.name}} {{item.price}}</ion-label>
<ion-label slot="end" class="ion-text-end price-add">
<ion-text color="medium">+ {{prop}} ريال </ion-text>
</ion-label>
</ion-item>
推荐阅读
- azure-devops - 列出 ADO 的所有用户权利
- flyway - FlywayException:不支持的数据库:MariaDB 10.4
- css - 如何修复 svg CSS 动画?
- vue.js - Implementing a router in Vue 2 project
- http - 为什么不是所有的 cookie 都设置为 HTTPONLY 和 SECURE
- sql - 添加 RETURNING 语句导致 ORA-00933: SQL 命令未正确结束
- arrays - 数组序列(字符串)返回下一个可见按钮 - React
- go - 连接到 grafana loki
- javascript - 如何在输入文本字段中显示值?
- android - Flutter - RevenueCat - 存在凭据问题,请检查底层错误以获取更多详细信息,购买后出现错误