Dynamically create object keys in IE 11 (expected identifier, string or number, not comma)

I am looking for a solution that dynamically creates object keys (are they spelled correctly?).

Arbitrary example, but it works in chrome and firefox

var weeks = {} for(var i = 0; i < 5; i++){ $.extend(weeks, {["week" + i] : (i * 2)} } //weeks = {"week0":0,"week1":2,"week2":4,"week3":6,"week4":8} 

Or an alternative arbitrary example

 var object = { ["a" + 50]: "value" } 

The problem seems to be related to the [] operator, but I do not understand how and why this problem only occurs in IE. I did not test in previous versions of IE11, but I would assume that the problem persists there.

Since the problem seems to be related to the [] operator itself, creating my keys in a variable and then dragging this variable into my [] will not do anything to fix this problem, so I seem to have both ideas and keywords for google.

So, is there a way to dynamically create object keys in IE?

+5
source share
1 answer

IE11 is not a β€œmodern” web browser like Chrome, Firefox, or even Edge. It does not support the new "object literals" from ES6 (ES2015).

The syntax you use is called "computed property keys"; you cannot use it in IE11. You need to do it in the "old fashioned" way.

 var weeks = {}; for(var i = 0; i < 5; i++){ var tmp = {}; tmp["week" + i] = i*2; $.extend(weeks, tmp); } 
+5
source

All Articles