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
}
}
)
}
You have to do it the elaborate way in ES5:
(or move the creation of
obj
inside the loop if it's different for each iteration)If you happen to have code full of ES6+ syntax, such as computed property names, and you want to make it ES5 compatible, the easiest way by far would be to use a transpiler like Babel to do it for you automatically. This will allow you to write your source code in the latest and most readable version of the language while permitting obsolete browsers to understand the transpiled code, without having to mess with ugly and verbose ES5 syntax yourself.
For example, if you had source code that looks like this that you wanted to make ES5 compatible:
You can run it through Babel and automatically get the ES5 version:
The transpiled code might look a bit ugly, but most of the time, you don't even need to look at it, just serve it to clients and it'll work, even on obsolete environments like Internet Explorer.