How do I remove the object in my array subBrands that is nested inside another array where the id property of an object is = to 31. I'm trying to get the whole parent array back without that subBrand removed.
Array is:
[
{
"id": 10,
"name": "Parent Brand 1",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 09:55:51",
"updated_at": "2017-02-02 09:55:51",
"subBrands": [
{
"id": 31,
"name": "Sub Brand 6",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:24:49",
"updated_at": "2017-02-02 11:42:02"
},
{
"id": 32,
"name": "Sub Brand 7",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:24:57",
"updated_at": "2017-02-02 11:42:18"
},
{
"id": 33,
"name": "Sub Brand 8",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:25:04",
"updated_at": "2017-02-02 11:42:34"
},
{
"id": 34,
"name": "Sub Brand 9",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:25:39",
"updated_at": "2017-02-02 11:42:43"
},
{
"id": 35,
"name": "Sub Brand 10",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:25:46",
"updated_at": "2017-02-02 11:42:52"
},
{
"id": 36,
"name": "Sub Brand 4",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:43:53",
"updated_at": "2017-02-02 11:43:53"
}
]
},
{
"id": 12,
"name": "Parent Brand 2",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 09:56:16",
"updated_at": "2017-02-02 09:56:16",
"subBrands": []
},
{
"id": 16,
"name": "Brand no children",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 10:37:40",
"updated_at": "2017-02-02 10:37:40",
"subBrands": []
},
{
"id": 37,
"name": "Whoops brand",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:44:10",
"updated_at": "2017-02-02 11:44:10",
"subBrands": []
}
]
What I'm trying to get is:
[
{
"id": 10,
"name": "Parent Brand 1",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 09:55:51",
"updated_at": "2017-02-02 09:55:51",
"subBrands": [
{
"id": 32,
"name": "Sub Brand 7",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:24:57",
"updated_at": "2017-02-02 11:42:18"
},
{
"id": 33,
"name": "Sub Brand 8",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:25:04",
"updated_at": "2017-02-02 11:42:34"
},
{
"id": 34,
"name": "Sub Brand 9",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:25:39",
"updated_at": "2017-02-02 11:42:43"
},
{
"id": 35,
"name": "Sub Brand 10",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:25:46",
"updated_at": "2017-02-02 11:42:52"
},
{
"id": 36,
"name": "Sub Brand 4",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:43:53",
"updated_at": "2017-02-02 11:43:53"
}
]
},
{
"id": 12,
"name": "Parent Brand 2",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 09:56:16",
"updated_at": "2017-02-02 09:56:16",
"subBrands": []
},
{
"id": 16,
"name": "Brand no children",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 10:37:40",
"updated_at": "2017-02-02 10:37:40",
"subBrands": []
},
{
"id": 37,
"name": "Whoops brand",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:44:10",
"updated_at": "2017-02-02 11:44:10",
"subBrands": []
}
]
I'm open to using underscores. The closest I'm come is:
var brands = _.filter(brands, function(n) {
return _.some(n.subBrands, function(subBrand){
return subBrand.id != brand.id;
});
});
But that removes the arrays that don't contain a subBrand with an id of 31. So it's not very close to what I need.
Cheers!
Use the splice () Method to Remove an Object From an Array in JavaScript The method splice () might be the best method out there that we can use to remove the object from an array. It changes the content of an array by removing or replacing existing elements or adding new elements in place. The syntax for the splice () method is shown below.
We explain nested arrays in a way that anyone, from beginner to expert, can understand. The Array in JavaScript, as with arrays in other scripting and programming languages, is an object that lets you store multiple items in an ordered list, with each item having its own index.
You can remove elements from the end of an array using pop, from the beginning using shift, or from the middle using splice. The JavaScript Array filter method to create a new array with desired items, a more advanced way to remove unwanted elements.
You can use for..of loop to iterate each subBrands array, Array.prototype.splice () to remove object having id 31 from array. Thanks for contributing an answer to Stack Overflow!
var arr = [{"id":10,"name":"Parent Brand 1","parent":null,"author":1,"deleted_at":null,"created_at":"2017-02-02 09:55:51","updated_at":"2017-02-02 09:55:51","subBrands":[{"id":31,"name":"Sub Brand 6","parent":10,"author":1,"deleted_at":null,"created_at":"2017-02-02 11:24:49","updated_at":"2017-02-02 11:42:02"},{"id":32,"name":"Sub Brand 7","parent":10,"author":1,"deleted_at":null,"created_at":"2017-02-02 11:24:57","updated_at":"2017-02-02 11:42:18"},{"id":33,"name":"Sub Brand 8","parent":10,"author":1,"deleted_at":null,"created_at":"2017-02-02 11:25:04","updated_at":"2017-02-02 11:42:34"},{"id":34,"name":"Sub Brand 9","parent":10,"author":1,"deleted_at":null,"created_at":"2017-02-02 11:25:39","updated_at":"2017-02-02 11:42:43"},{"id":35,"name":"Sub Brand 10","parent":10,"author":1,"deleted_at":null,"created_at":"2017-02-02 11:25:46","updated_at":"2017-02-02 11:42:52"},{"id":36,"name":"Sub Brand 4","parent":10,"author":1,"deleted_at":null,"created_at":"2017-02-02 11:43:53","updated_at":"2017-02-02 11:43:53"}]},{"id":12,"name":"Parent Brand 2","parent":null,"author":1,"deleted_at":null,"created_at":"2017-02-02 09:56:16","updated_at":"2017-02-02 09:56:16","subBrands":[]},{"id":16,"name":"Brand no children","parent":null,"author":1,"deleted_at":null,"created_at":"2017-02-02 10:37:40","updated_at":"2017-02-02 10:37:40","subBrands":[]},{"id":37,"name":"Whoops brand","parent":null,"author":1,"deleted_at":null,"created_at":"2017-02-02 11:44:10","updated_at":"2017-02-02 11:44:10","subBrands":[]}];
var id = prompt("Id of subbrands to remove: ");
arr.forEach(function(o) {
o.subBrands = o.subBrands.filter(s => s.id != id);
});
console.log(arr);
You could iterate the parent part, and the children and if found splice the object.
var data = [{ id: 10, name: "Parent Brand 1", parent: null, author: 1, deleted_at: null, created_at: "2017-02-02 09:55:51", updated_at: "2017-02-02 09:55:51", subBrands: [{ id: 31, name: "Sub Brand 6", parent: 10, author: 1, deleted_at: null, created_at: "2017-02-02 11:24:49", updated_at: "2017-02-02 11:42:02" }, { id: 32, name: "Sub Brand 7", parent: 10, author: 1, deleted_at: null, created_at: "2017-02-02 11:24:57", updated_at: "2017-02-02 11:42:18" }, { id: 33, name: "Sub Brand 8", parent: 10, author: 1, deleted_at: null, created_at: "2017-02-02 11:25:04", updated_at: "2017-02-02 11:42:34" }, { id: 34, name: "Sub Brand 9", parent: 10, author: 1, deleted_at: null, created_at: "2017-02-02 11:25:39", updated_at: "2017-02-02 11:42:43" }, { id: 35, name: "Sub Brand 10", parent: 10, author: 1, deleted_at: null, created_at: "2017-02-02 11:25:46", updated_at: "2017-02-02 11:42:52" }, { id: 36, name: "Sub Brand 4", parent: 10, author: 1, deleted_at: null, created_at: "2017-02-02 11:43:53", updated_at: "2017-02-02 11:43:53" }] }, { id: 12, name: "Parent Brand 2", parent: null, author: 1, deleted_at: null, created_at: "2017-02-02 09:56:16", updated_at: "2017-02-02 09:56:16", subBrands: [] }, { id: 16, name: "Brand no children", parent: null, author: 1, deleted_at: null, created_at: "2017-02-02 10:37:40", updated_at: "2017-02-02 10:37:40", subBrands: [] }, { id: 37, name: "Whoops brand", parent: null, author: 1, deleted_at: null, created_at: "2017-02-02 11:44:10", updated_at: "2017-02-02 11:44:10", subBrands: [] }];
data.some(function (a) {
return a.subBrands.some(function (b, i, bb) {
if (b.id === 31) {
bb.splice(i, 1);
return true;
}
});
});
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I nest two forEach
loops and return when the item has been found and removed:
let items = [{id: 1, subItems: [{id: 1}, {id: 2}]}];
const subItemToBeRemovedId = 1;
items.forEach((item) => item.subItems.forEach((subItem, index) => {
if (subItem.id === subItemToBeRemovedId) {
return item.subItems.splice(index, 1);
}
}));
console.log(items);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With