admin管理员组文章数量:1312774
I'm looking for a solution that creates object keys (is that worded correctly?) dynamically.
Arbitrary example, but this 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 alternative arbitrary example
var object = {
["a" + 50]: "value"
}
The problem seem to be rooted in the []
operator, but I don't understand how or why this problem only occurs in IE. I have not tested in previous versions to IE11, but I would assume the problem would persist there aswell.
Since the problem seem to be with the []
operator itself, creating my keys in a variable and then shoving that variable into my [] wouldn't do anything to fix the problem, so I seem to be both out of ideas and keywords to google.
So is there a way to dynamically create object keys in IE?
I'm looking for a solution that creates object keys (is that worded correctly?) dynamically.
Arbitrary example, but this 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 alternative arbitrary example
var object = {
["a" + 50]: "value"
}
The problem seem to be rooted in the []
operator, but I don't understand how or why this problem only occurs in IE. I have not tested in previous versions to IE11, but I would assume the problem would persist there aswell.
Since the problem seem to be with the []
operator itself, creating my keys in a variable and then shoving that variable into my [] wouldn't do anything to fix the problem, so I seem to be both out of ideas and keywords to google.
So is there a way to dynamically create object keys in IE?
Share Improve this question edited May 23, 2016 at 13:48 NachoDawg asked May 23, 2016 at 13:42 NachoDawgNachoDawg 1,54412 silver badges21 bronze badges 5- 2 IE11 doesn't support many (if any) of the new ES6 syntaxes. Remember, it's Internet Explorer, not a modern web browser. – gen_Eric Commented May 23, 2016 at 13:48
- 2 Extending on @RocketHazmat ment: check "object literal extensions -> puted properties" in the ECMAScript6 patibility table to check where you can use this syntax. – apokryfos Commented May 23, 2016 at 13:50
-
2
Why use extend at all? Just use the square bracket notation and assign the property directly?
weeks["week" + i] = i * 2;
– evolutionxbox Commented May 23, 2016 at 13:52 - Because the actual object I'm looking to create is bigger and more plicated than my examples in the question. My problem is not the use of $.extend. – NachoDawg Commented May 23, 2016 at 13:54
- stackoverflow./questions/1184123/… - What problem are you actually facing in IE11 then? Does it throw any errors? – evolutionxbox Commented May 23, 2016 at 14:00
1 Answer
Reset to default 10IE11 is not a "modern" web browser in the same way Chrome, Firefox or even Edge is. It doesn't support the new "object literal extensions" from ES6 (ES2015).
The syntax you are using is called "puted property keys", you cannot use it in IE11. You need to do this the "old fashioned" way.
var weeks = {};
for(var i = 0; i < 5; i++){
var tmp = {};
tmp["week" + i] = i*2;
$.extend(weeks, tmp);
}
本文标签:
版权声明:本文标题:javascript - Dynamically create object keys in IE 11 (Expected identifier, string or number, not a comma issue) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741888169a2403147.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论