[英]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.