有一個API會返回這樣的數(shù)據(jù)[ { "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" } ] }]服務(wù)getMenus() { this.dataServices.menuList(this.pagedData) .subscribe( response => { if (response && response.code === HttpStatus.OK) { this.dataList = response.data; } }, );}我目前正在嘗試做的是能夠從服務(wù)器返回的數(shù)據(jù)中刪除子對象,因此我具有此功能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); } }}當(dāng)我調(diào)用該函數(shù)時,出現(xiàn)此錯誤 ERROR TypeError: Cannot read property 'length' of undefined
從帶有angular6的對象中刪除子對象
蕪湖不蕪
2021-05-13 10:15:49