I am trying to create a function in TypeScript that will return an object like this:
marked = {
'2024-08-21': {
dots:[food, recycling]
},
'2024-08-22': {
dots:[food, recycling]
}
}
Here is the code I have written so far.
function addKeysAndValuesDynamically(days: string[]): any {
var marked = {};
const food = {key: 'food', color: 'brown'};
const recycling = {key: 'recycling', color: 'green'};
days.forEach((day: string) => {
marked[day]['dots'] = food, recycling;
});
return marked;
}
var days: string[] = ['2024-08-21', '2024-08-22'];
var marked = addKeysAndValuesDynamically(days);
console.log(`marked = ${marked}`);
However, when I run the code, I receive an error message:
TypeError: Cannot set property 'dots' of undefined
.