I have an array of objects with this structure
const arr = [{
id: 0,
name: 'string'
}, {
id: 1,
name: 'string'
}]
I also have main array
const mainArray = [{
items: [{
id: 0,
name: 'string'
}]
},
{
items: [{
id: 5,
name: 'string'
},
{
id: 3,
name: 'string'
}
]
}
];
I'm displaying every item in items arrays from mainArray on the screen.
What I want to do is remove every item of arr from every items array of mainArray and show updated mainArray on the screen.
I was thinking doing something like this
mainArray = mainArray.map((e) => {
e.items = e.items.filter((el) => {
return !arr.includes( el );
});
});
This returns an error Cannot read property 'items' of undefined
Or something like this
mainArray = mainArray.filter((e) => {
return !arr.includes( e.items );
});
But this also doesn't work.
All the help will be appreciated.
mainArray = ...since you used theconstkeyword to define that variable. it is a constant and cannot be overridden.