javascript - 用户点击 angular6 后获取所有子对象 ID 的列表
问题描述
我正在页面上显示来自 API 的记录列表。该页面同时显示父对象和子对象。
JSON数据
const dataList = [
{
"id": 9,
"name": "Past Menu",
"serveDate": "2019-05-08 00:00:00",
"meals": [
{
"id": 27,
"name": "6",
"description": "6",
"image": "",
"mealType": "BREAKFAST",
"unitPrice": 6,
"status": "ENABLED"
},
{
"id": 28,
"name": "7",
"description": "7",
"image": "",
"mealType": "BREAKFAST",
"unitPrice": 7,
"status": "ENABLED"
},
{
"id": 30,
"name": "9",
"description": "9",
"image": "",
"mealType": "BREAKFAST",
"unitPrice": 9,
"status": "ENABLED"
}
]
},
{
"id": 8,
"name": "Good Menu",
"serveDate": "2019-05-10 00:00:00",
"meals": [
{
"id": 28,
"name": "7",
"description": "7",
"image": "",
"mealType": "BREAKFAST",
"unitPrice": 7,
"status": "ENABLED"
},
{
"id": 30,
"name": "9",
"description": "9",
"image": "",
"mealType": "BREAKFAST",
"unitPrice": 9,
"status": "ENABLED"
},
{
"id": 31,
"name": "10",
"description": "10",
"image": "",
"mealType": "BREAKFAST",
"unitPrice": 10,
"status": "ENABLED"
}
]
}
];
HTML 视图
<div *ngFor="let item of menuList">
<h2 (click)="getMealsIds()">Menu</h2>
{{item.name}} - {{item.servedate}}
<h2>Meals</h2>
<div *ngFor="let meal of item.meals">
<span (click)="removeMeal(meal, item.id)">{{meal.name}} - {{meal.mealType}}</span>
</div>
</div>
现在我想要实现的是,当单击特定菜单时,我应该能够获取数组中的所有餐点 ID
功能
getMealsIds(parent) {
console.log(parent.meals.id)
}
有关如何使这项工作正常进行的任何帮助?使用我当前的脚本单击时出现未定义的错误
解决方案
你想要map
的数组。如果parent
是dataList[0]
那么
console.log(parent.meals.map(m => m.id)); // [27, 28, 30]
推荐阅读
- parallel-processing - WebGL - 从纹理读取后有缺失值
- excel - Excel中的选择性下拉人口
- wordpress - 删除插件类中定义的操作
- python - docker-py 中 docker port CONTAINER 的等效命令是什么
- python - Webscraper 没有得到文本
- linux-kernel - PCIe 内核驱动程序 DMA 写入不正确的地址
- python - Python将标签输入笔记本部分
- angular - 架构验证失败并出现以下错误:数据路径“”应该具有必需的属性“tsConfig”
- python - 谁能解释一下这两个代码之间的区别
- java - 在 Java NetBeans 中按下时,按 EnterPad 不能用作提交按钮