let weekData = [];
for (let i = 0; i < 7; i++) {
const weekdayName = moment().subtract('days', i).format('dddd');
weekData.push({
[weekdayName]: 0
});
}
I'm trying to create a list of objects with dynamic property names. The code above creates objects with properties named _name
. How can I modify it to achieve this? I attempted using square brackets like this:
{ moment().subtract('days', i).format('dddd') : 0 }
, but encountered a syntax error. Can someone help me figure out the correct approach?