user2834172 user2834172 - 3 months ago 19
Javascript Question

How to use a computed property name in ES5?

I would like to have a computed property name. I saw you can have this in ES6. But it should be compatible with IOS Webview. So I can't use ES6. Also the
computed name will be ever the same inside the loop, if this makes it easier for somebody.

Any Ideas?

var today = moment().format('DD.MM.YY');
for (var i = 0; i < 5; i++) {
initialData.push(
{
dates: {
"01.01.01": false
// instead of 01.01.01 i would like to have the value of today as the key
}
}
)
}

Answer

You have to do it the elaborate way in ES5:

var today  = moment().format('DD.MM.YY');
var obj    = {};
obj[today] = false;
for (var i = 0; i < 5; i++) {
  initialData.push({ dates: obj });
}

(or move the creation of obj inside the loop if it's different for each iteration)