简体   繁体   English

如何从数组中对象内部的数组中删除此对象?

[英]how to remove this object from array inside of object in array?

I am not sure how to form this question, but I will do my best. 我不确定如何提出这个问题,但我会尽力而为。 I don't know how to remove object by _id from 'list:' part. 我不知道如何通过_id从“列表:”部分中删除对象。 So, I have one array, and inside of that array I have list of objects,inside of these objects I have again array with objects, so I want to remove one object from that last array, how I can do that? 因此,我有一个数组,并且在该数组中有对象列表,在这些对象中,我又有一个对象数组,因此我想从最后一个数组中删除一个对象,我该怎么做? Cannot fix it for 2 days, I'm stucked! 2天无法修复,我被卡住了! Thanks! 谢谢!

[
  {
    "_id": "599a1344bf50847b0972a465",
    "title": "British Virgin Islands BC",
    "list": [],
    "price": "1350"
  },
  {
    "_id": "599a1322bf50847b0972a38e",
    "title": "USA (Nevada) LLC",
    "list": [
      {
        "_id": "599a1322bf50847b0972a384",
        "title": "Nominee Member",
        "service": "nominee-service",
        "price": "300"
      },
      {
        "_id": "599a1322bf50847b0972a385",
        "title": "Nominee Manager & General Power of Attorney (Apostilled)",
        "service": "nominee-service",
        "price": "650"
      },
      {
        "_id": "599a1322bf50847b0972a386",
        "title": "Special Power of Attorney",
        "service": "nominee-service",
        "price": "290"
      }
    ],
    "price": "789"
  },
  {
    "_id": "599a12fdbf50847b0972a2ad",
    "title": "Cyprus LTD",
    "list": [
      {
        "_id": "599a12fdbf50847b0972a2a5",
        "title": "Nominee Shareholder",
        "service": "nominee-service",
        "price": "370"
      },
      {
        "_id": "599a12fdbf50847b0972a2a6",
        "title": "Nominee Director & General Power or Attorney (Apostilled)",
        "service": "nominee-service",
        "price": "720"
      },
      {
        "_id": "599a12fdbf50847b0972a2ab",
        "title": "Extra Rubber Stamp",
        "service": "other-service",
        "price": "40"
      }
    ],
    "price": "1290"
  }
]

Using Vanilla JS: 使用Vanilla JS:

 function findAndRemove(data, id) { data.forEach(function(obj) { // Loop through each object in outer array obj.list = obj.list.filter(function(o) { // Filter out the object with unwanted id, in inner array return o._id != id; }); }); } var data = [{ "_id": "599a1344bf50847b0972a465", "title": "British Virgin Islands BC", "list": [], "price": "1350" }, { "_id": "599a1322bf50847b0972a38e", "title": "USA (Nevada) LLC", "list": [{ "_id": "599a1322bf50847b0972a384", "title": "Nominee Member", "service": "nominee-service", "price": "300" }, { "_id": "599a1322bf50847b0972a385", "title": "Nominee Manager & General Power of Attorney (Apostilled)", "service": "nominee-service", "price": "650" }, { "_id": "599a1322bf50847b0972a386", "title": "Special Power of Attorney", "service": "nominee-service", "price": "290" } ], "price": "789" }, { "_id": "599a12fdbf50847b0972a2ad", "title": "Cyprus LTD", "list": [{ "_id": "599a12fdbf50847b0972a2a5", "title": "Nominee Shareholder", "service": "nominee-service", "price": "370" }, { "_id": "599a12fdbf50847b0972a2a6", "title": "Nominee Director & General Power or Attorney (Apostilled)", "service": "nominee-service", "price": "720" }, { "_id": "599a12fdbf50847b0972a2ab", "title": "Extra Rubber Stamp", "service": "other-service", "price": "40" } ], "price": "1290" } ]; // Empty almost all of list, except middle one findAndRemove(data, "599a1322bf50847b0972a384"); findAndRemove(data, "599a1322bf50847b0972a386"); findAndRemove(data, "599a12fdbf50847b0972a2a5"); findAndRemove(data, "599a12fdbf50847b0972a2a6"); findAndRemove(data, "599a12fdbf50847b0972a2ab"); console.log(data); 

Cleared everything except middle list, just for better visualization. 清除除中间列表以外的所有内容,只是为了更好地可视化。

You can use Array.map and Array.filter to accomplish this. 您可以使用Array.mapArray.filter来完成此任务。 Detailed explanation in comments: 评论中的详细说明:

PS: This snippet uses ES6 arrow functions and spread operator PS:此代码段使用ES6 箭头功能传播算子

function removeById(arr, id) {
  // Array.map iterates over each item in the array,
  // and executes the given function on the item.
  // It returns an array of all the items returned by the function.
  return arr.map(obj => {

    // Return the same object, if the list is empty / null / undefined
    if (!obj.list || !obj.list.length) return obj;

    // Get a new list, skipping the item with the spedified id
    const newList = obj.list.filter(val => val._id !== id);

    // map function returns the new object with the filtered list
    return { ...obj, list: newList };
  });
}

const oldArray = <YOUR_ORIGINAL_ARRAY>;
const newArray = removeById(arr, "599a12fdbf50847b0972a2a5");

@Abhijit Kar your one is working perfectly, thanks mate! @Abhijit Kar您的一个工作正常,谢谢队友! How I can later splice this list? 以后如何拼接此列表? When I was working with objects from first array, I did it like this : 当我处理第一个数组中的对象时,我这样做是这样的:

var inventory = jsonArrayList;    
for (var i = 0; i < inventory.length; i++) {
            if (inventory[i]._id == deleteProductById) {
              vm.items.splice(i, 1);
              break;
            }
          }

It would be very helpful, thanks alot! 这将非常有帮助,非常感谢!

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

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