const eventsByDay = {
"17 May": [
// some objects
],
"18 May": [
// some objects
],
"19 May": [
// some objects
]
};
const timeIntervals = [
{ timeStart: "10:00", timeFinish: "11:00" },
{ timeStart: "11:00", timeFinish: "12:00" },
{ timeStart: "12:00", timeFinish: "13:00" },
{ timeStart: "13:00", timeFinish: "14:00" }
];
const eventsDefaultConfig = {
slidesPerView: "auto",
spaceBetween: 16,
navigation: {
nextEl: "",
prevEl: ""
}
};
let eventsConfigs = {};
for (const day in eventsByDay) {
const dayNumber = day.split(" ", 1)[0];
timeIntervals.forEach(interval => {
const configName = `${dayNumber}-${interval.timeStart}`;
console.log(configName);
eventsConfigs[configName] = eventsDefaultConfig;
eventsConfigs[configName].navigation.nextEl = `${configName}-next-slide`;
eventsConfigs[configName].navigation.prevEl = `${configName}-prev-slide`;
});
}
console.log(eventsConfigs);
I'm trying to create objects with auto generated values. I'm using for...of to iterate main object (eventsByDay) and nested forEach loop to iterate array with time intervals. My goal is object that contains following structure and naming (each element in navigation object has unique name based on date from eventByDay and time interval from array)
{
"17-10:00": {
"slidesPerView": "auto",
"spaceBetween": 16,
"navigation": {
"nextEl": "17-10:00-next-slide",
"prevEl": "17-10:00-prev-slide"
}
},
"17-11:00": {
"slidesPerView": "auto",
"spaceBetween": 16,
"navigation": {
"nextEl": "17-11:00-next-slide",
"prevEl": "17-11:00-prev-slide"
}
},
"17-12:00": {
"slidesPerView": "auto",
"spaceBetween": 16,
"navigation": {
"nextEl": "17-12:00-next-slide",
"prevEl": "17-12:00-prev-slide"
}
},
...
}
When I check console messages and values of configName everything seems to be good, but final result contains only object values with last value from array. What should I fix to get proper result?
{
"17-10:00": {
"slidesPerView": "auto",
"spaceBetween": 16,
"navigation": {
"nextEl": "19-13:00-next-slide",
"prevEl": "19-13:00-prev-slide"
}
},
"17-11:00": {
"slidesPerView": "auto",
"spaceBetween": 16,
"navigation": {
"nextEl": "19-13:00-next-slide",
"prevEl": "19-13:00-prev-slide"
}
},
"17-12:00": {
"slidesPerView": "auto",
"spaceBetween": 16,
"navigation": {
"nextEl": "19-13:00-next-slide",
"prevEl": "19-13:00-prev-slide"
}
},
...
}