有一个API会返回这样的数据
[
{
"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": "Bomb 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"
}
]
}
]
服务
getMenus() {
this.dataServices.menuList(this.pagedData)
.subscribe(
response => {
if (response && response.code === HttpStatus.OK) {
this.dataList = response.data;
}
},
);
}
我目前正在尝试做的是能够从服务器返回的数据中删除子对象,因此我具有此功能
deleteItem(item) {
for (let r = 0; r < this.dataList.meals.length; r++) {
if (this.dataList.meals[r].id === item.id) {
this.dataList.meals.splice(r, 1);
}
}
}
当我调用该函数时,出现此错误 ERROR TypeError: Cannot read property 'length' of undefined
相关分类