[英]how to remove this object from array inside of object in array?
我不确定如何提出这个问题,但我会尽力而为。 我不知道如何通过_id从“列表:”部分中删除对象。 因此,我有一个数组,并且在该数组中有对象列表,在这些对象中,我又有一个对象数组,因此我想从最后一个数组中删除一个对象,我该怎么做? 2天无法修复,我被卡住了! 谢谢!
[
{
"_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"
}
]
使用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);
清除除中间列表以外的所有内容,只是为了更好地可视化。
您可以使用Array.map和Array.filter来完成此任务。 评论中的详细说明:
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您的一个工作正常,谢谢队友! 以后如何拼接此列表? 当我处理第一个数组中的对象时,我这样做是这样的:
var inventory = jsonArrayList;
for (var i = 0; i < inventory.length; i++) {
if (inventory[i]._id == deleteProductById) {
vm.items.splice(i, 1);
break;
}
}
这将非常有帮助,非常感谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.