简体   繁体   English

删除数组项内的对象

[英]Remove object inside array items

I have two array of objects like so:我有两个对象数组,如下所示:

Main Array:主阵列:

 [ { "controller": "Education", "items": [ { "name": "Insert", "controller": "Education", "id": 1 }...etc ] }, { "controller": "CustomerPackage", "items": [ { "name": "Insert", "controller": "CustomerPackage", "id": 1 }..etc ] } ]

Have the selected list similar as above list.选择类似于上面的列表。 if there are items in the selected list, I want to remove them from the main array如果所选列表中有项目,我想从主数组中删除它们

Here's what I've tried so far, any ideas?这是我到目前为止尝试过的,有什么想法吗?

this.mainArray.map(item => { 
  let subItemId = parseInt(item.items.map(i => i.id)); 
  this.selectedArray.map(v => { 
    v.items.map(j => { 
      if (subItemId == j.id) { 
        console.log("how can i make for mainArray list "); 
        console.log(j.name); 
      } 
    }) 
  })

You need to create an auxiliary variable to get the position of the item in the list you want to delete.您需要创建一个辅助变量来获取要删除的项目在列表中的位置。

 var myArray = [1, 2, 3, 4, 5, 6]; var positionList = -1; for (var i = 0; i < myArray.length; i++) { if (myArray[i] == 4) positionList = i; } myArray.splice(positionList, 1); alert(myArray);

Try it.尝试一下。

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

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