How would I go about searching a multidimensional array for a value and then delete the object where the value was found, the code below will not always be the same so i need a way to search all of the arrays to find the object with a specific value
For example, if I wanted to delete an object with the id of 8, how would I search all arrays and objects for the id and then delete the object, here is an example of the data
{
MenuLocation: 'Jersey',
MenuItems: [
{
id: '1',
parentId: '1',
position: 2,
name: 'test1',
link: 'http://google.com',
submenu: []
},
{
id: '2',
parentId: '2',
position: 1,
name: 'test2',
link: '#',
submenu: [
{
id: '3',
parentId: '2',
position: 1,
name: 'testsub1',
link: 'http://google.com',
submenu: []
},
{
id: '4',
parentId: '2',
position: 2,
name: 'testsub2',
link: 'http://google.com',
submenu: [
{
id: '5',
parentId: '4',
position: 1,
name: 'testsub4.1',
link: 'http://google.com',
submenu: []
},
{
id: '6',
parentId: '4',
position: 2,
name: 'testsub4.2',
link: 'http://google.com',
submenu: []
},
{
id: '7',
parentId: '4',
position: 3,
name: 'testsub4.3',
link: 'http://google.com',
submenu: [
{
id: '8',
parentId: '7',
position: 3,
name: 'testsub4.1',
link: 'http://google.com',
submenu: []
},
{
id: '9',
parentId: '7',
position: 2,
name: 'testsub4.2',
link: 'http://google.com',
submenu: []
},
{
id: '10',
parentId: '7',
position: 1,
name: 'testsub4.3',
link: 'http://google.com',
submenu: []
}
]
}
]
}
]
}
]
}
This is what I've put together so far, it's quite messy but it works but I assume this is not the best way to do it
delClick(id) {
for (var key in this.menu.MenuItems) {
var obj = this.menu.MenuItems[key];
if (obj.id == id) {
console.log('match');
} else {
if (obj.submenu.length > 0) {
for (var key in obj.submenu) {
var nextobj = obj.submenu[key];
if (nextobj.id == id) {
console.log('match');
console.log(nextobj);
} else {
for (var key in nextobj.submenu) {
var objn = nextobj.submenu[key]
if (objn.id == id) {
console.log('match');
console.log(objn);
}
}
}
}
}
}
}
} }
I would have to keep adding for loops until it checks everything but I don't know how many arrays and objects there will be, what is the best way to overcome this?