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. 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! 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:
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. Detailed explanation in comments:
PS: This snippet uses ES6 arrow functions and spread operator
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! 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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.