NOTE: I'm open to other solutions. The objective of this code is to easily get all events on a particular day by accessing it with a date. This might not be the greatest solution.
I want to push an "event" object into the correct array however I can't get it to work.
Below is the object that I want to "add" to.
this.state = {
events: {
Wed Apr 14 2021 00:00:00 GMT+0200 (Central European Summer Time): [],
Tue Apr 15 2021 00:00:00 GMT+0200 (Central European Summer Time): [],
and so on....
}
}
The "date-key"'s time is always the same (00:00:00 GMT+0200 (Central European Summer Time)) however not the day, month, or year.
And here is my attempt to get this to work:
addEvent(event) {
const events = this.state.events;
const date = event.date;
let list = [];
if (typeof events[date] !== 'undefined') {
list = events[date];
}
list.push(event);
//console.log(events[date]);
this.setState({
events: {
[date]: list
}
});
}
(The event parameter that is passed in is an object with a "new Date()" attribute as event.date.)
events: { [date]: list }as you can see in the addEvent function where date is a "new Date()". I'm however open to other solutions.