简体   繁体   English

从带有angular6的对象中删除子对象

[英]delete a child object from an object with angular6

There's an API which returns data like this 有一个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" } ] } ] 

SERVICES 服务

getMenus() {
  this.dataServices.menuList(this.pagedData)
  .subscribe(
    response => {
      if (response && response.code === HttpStatus.OK) {
        this.dataList = response.data;
      }
    },

  );
}

What i'm currently trying to do is to be able to delete a child object from the data returned from the server, so i have this function 我目前正在尝试做的是能够从服务器返回的数据中删除子对象,因此我具有此功能

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);
    }
  }
}

When i call the function i get this error ERROR TypeError: Cannot read property 'length' of undefined 当我调用函数时,出现此错误ERROR TypeError: Cannot read property 'length' of undefined

You need to go through all of your 'menu' items and then filter the 'meals' items as in the code below. 您需要检查所有“菜单”项,然后按照下面的代码过滤“餐”项。

 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": "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" } ] } ]; function deleteItem(item) { return dataList.map(menuItem => { const newItem = {...menuItem}; newItem.meals = newItem.meals.filter(meal => meal.id !== item.id); return newItem; }); } const newList = deleteItem({id:30}); console.log(JSON.stringify(newList,0,2)); 

return dataList.map(

Walks through the outer list of menu items and allows a modified value to be returned. 遍历menu项的外部列表,并允许返回修改后的值。

newItem.meals = newItem.meals.filter(meal => meal.id !== item.id);

Goes through all of the meals items and returns false if your selected ID matched the meal id, in this case 30 . 遍历所有meals项目,如果您选择的ID与进餐ID相匹配(在这种情况下为30 ,则返回false。 If the value returned is true then the meals item is included in the filter. 如果返回的值为true,则meals项将包含在过滤器中。 Otherwise it is excluded. 否则将被排除。

You should iterate over this.dataList: 您应该遍历this.dataList:

deleteItem(item){
    for (let i = 0; i < this.dataList.length; i++) {
        for (let r = 0; r < element.meals.length; r++) {
            if (this.dataList[i].element.meals[r].id === item.id) {
                this.dataList[i].element.meals.splice(r, 1);
            }
        }
    }
}        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM