Javascript 如何从变量设置 JS 对象属性名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13833204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To Set A JS object property name from a variable
提问by Vinoth Babu
I am in need to set a JS object property name dynamically.
我需要动态设置一个 JS 对象属性名称。
for(i=1; i<3; i++) {
var key = i+'name';
data = {
key : 'name1',
}
}
Result should be:
结果应该是:
data = {
1name: 'name1'
2name: 'name1'
}
回答by closure
var jsonVariable = {};
for(var i=1; i < 3; i++) {
jsonVariable[i + 'name'] = 'name' + i;
}
回答by Musa
You'll have to use []notation to set keys dynamically.
您必须使用[]符号来动态设置键。
var jsonVariable = {};
for(i=1; i<3; i++) {
var jsonKey = i+'name';
jsonVariable[jsonKey] = 'name1';
}
Now in ES6 you can use object literal syntax to create object keys dynamically, just wrap the variable in []
现在在 ES6 中,您可以使用对象字面量语法动态创建对象键,只需将变量包装在 []
var key = i + 'name';
data = {
[key] : 'name1',
}
回答by chiliNUT
With ECMAScript 6, you can use variable property names with the object literal syntax, like this:
使用 ECMAScript 6,您可以使用具有对象字面量语法的变量属性名称,如下所示:
var keyName = 'myKey';
var obj = {
[keyName]: 1
};
obj.myKey;//1
This syntax is available in the following newer browsers:
此语法在以下较新的浏览器中可用:
Edge 12+ (No IE support), FF34+, Chrome 44+, Opera 31+, Safari 7.1+
Edge 12+(不支持 IE)、FF34+、Chrome 44+、Opera 31+、Safari 7.1+
(https://kangax.github.io/compat-table/es6/)
( https://kangax.github.io/compat-table/es6/)
You can add support to older browsers by using a transpilersuch as babel. It is easy to transpile an entire project if you are using a module bundler such as rollupor webpack.
您可以使用转译器(例如babel )来添加对旧浏览器的支持。如果您使用诸如rollup或webpack 之类的模块捆绑器,则很容易转译整个项目。
回答by timc
This is the way to dynamically set the value
这是动态设置值的方式
var jsonVariable = {};
for (var i = 1; i < 3; i++) {
var jsonKey = i + 'name';
jsonVariable[jsonKey] = 'name' + i;
}
回答by Boris Detry
Use a variable as an object key
使用变量作为对象键
let key = 'myKey';
let data = {[key] : 'name1'; }
See How to iterete on your object here
请参阅如何在此处迭代您的对象
回答by Knut Hering
It does not matter where the variable comes from. Main thing we have one ... Set the variable name between square brackets "[ .. ]".
变量来自哪里并不重要。我们有一个主要的东西......在方括号“[ .. ]”之间设置变量名称。
var optionName = 'nameA';
var JsonVar = {
[optionName] : 'some value'
}
回答by suman
jsonVariable = {}
for(i=1; i<3; i++) {
var jsonKey = i+'name';
jsonVariable[jsonKey] = 'name1'
}
this will be similar to
这将类似于
jsonVariable = {
1name : 'name1'
2name : 'name1'
}
回答by Mark Corrigan
Along the lines of Sainath S.R's comment above, I was able to set a js object property name from a variable in Google Apps Script (which does not support ES6 yet) by defining the object then defining another key/value outside of the object:
按照上面 Sainath SR 的评论,我能够通过定义对象然后在对象之外定义另一个键/值,从 Google Apps 脚本(尚不支持 ES6)中的变量设置 js 对象属性名称:
var salesperson = ...
var mailchimpInterests = {
"aGroupId": true,
};
mailchimpInterests[salesperson] = true;

