I am new to js and struggle with objects and arrays within them. I got months and days in an event-object events[month] = {dates: []} like this:
events: (2) […]
5: {…}
dates: (9) […]
0: 5
1: 12
2: 19
3: 26
4: 3
5: 10
6: 17
7: 24
8: 31
length: 9
<prototype>: []
<prototype>: {…}
6: {…}
dates: (9) […]
0: 2
1: 9
2: 16
3: 23
4: 30
5: 7
6: 14
7: 21
8: 28
length: 9
<prototype>: []
<prototype>: {…}
And now I want to delete several dates:
month: 5, day: 24, 26
month: 6, day: 14, 16
Do I have to make several loops to get this done? I tried some for() and if() statements, but somehow the logic is tricky. Are there other methods to do this? You help is greatly appreciated.
You can use recursion for this if you don't know nesting of objects. In your case you have to use forEach method to find needful elements. But remember to not to mutate starting array.
const newEvents = events;
events.forEach((item, i) => {
if (i === 5) {
item.dates.forEach((innerItem, j) => {
if (innerItem === 24) newEvents.splice(j, 1);
})
}
})
return newEvents;