I have an array of objects. Each object can also contain an array of objects, and so on to an arbitrary depth.
var myArray = [
{
id:'foo',
items:[]
},
{
id:'bar',
items:[
{
id:'blah'
items:[...etc...]
}
]
}
]
I'd like to read, add, and remove objects in the nested arrays using an array of indices.
So a function to remove the value myArray[1][3][2] from myArray would be called with this array of indexes as a parameter: [1, 3, 2]
I've found that you can use reduce() to return a value like so:
indices.reduce((acc, cur) => Array.isArray(acc) ? acc[cur] : acc.items[cur], myArray)
but cannot work out how to remove or add a value using the same idea. Any help is much appreciated.