Struggling to wrap my head around this, not sure if it's feasible in this manner but I just can't seem to grasp it.
I have an array of objects where each object contains some values and another array of objects. Essentially, I need to filter the main array and remove a single object from the events array.
Below is the data array:
let data = [
{
id: 1,
date: '2023-10-01',
events: [{ id: '1a', name: 'event 1' }, { id: '1b', name: 'event 2' }],
},
{
id: 2,
date: '2023-10-02',
events: [{ id: '2a', name: 'event 1' }, { id: '2b', name: 'event 2' }],
},
{
id: 3,
date: '2023-10-03',
events: [{ id: '3a', name: 'event 1' },{ id: '3b', name: 'event 2' }],
},
];
An example of what I want to achieve is to filter the data so that if the id == 1, then remove event with name `event 1`.
let data = [
{
id: 1,
date: '2023-10-01',
events: [{ id: '1a', name: 'event 1'}],
},
{
id: 2,
date: '2023-10-02',
events: [{ id: '2a', name: 'event 1' }, { id: '2b', name: 'event 2' }],
},
{
id: 3,
date: '2023-10-03',
events: [{ id: '3a', name: 'event 1' },{ id: '3b', name: 'event 2' }],
},
];
Here's what I've attempted so far, but struggling to make it work:
console.log(data.filter((item) => item.id !== '1')[0].events.filter((event) => event.id != '1b'))
I also tried filtering then filtering again, but no luck so far:
const filtered = (data.filter((item) => item.id == '1'))
console.log(filtered.filter((event) => event.id == '1a'))
Any suggestions or help would be appreciated. Thank you!