如何从 JavaScript 中的变量值创建对象属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2241875/
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 create an object property from a variable value in JavaScript?
提问by ecu
I want to add a new property to 'myObj', name it 'string1' and give it a value of 'string2', but when I do it it returns 'undefined:
我想向“myObj”添加一个新属性,将其命名为“string1”并为其赋值为“string2”,但是当我这样做时,它返回“undefined”:
var myObj = new Object;
var a = 'string1';
var b = 'string2';
myObj.a = b;
alert(myObj.string1); //Returns 'undefined'
alert(myObj.a); //Returns 'string2'
In other words: How do I create an object property and give it the name stored in the variable, but not the name of the variable itself?
换句话说:如何创建对象属性并为其指定存储在变量中的名称,而不是变量本身的名称?
回答by philfreo
回答by Oriol
ES6 introduces computed property names, which allow you to do
ES6 引入了计算属性名称,它允许你做
var myObj = {[a]: b};
Note browser support is currently negligible.
注意浏览器支持目前可以忽略不计。
回答by cgp
Dot notation and the properties are equivalent. So you would accomplish like so:
点符号和属性是等价的。所以你会像这样完成:
var myObj = new Object;
var a = 'string1';
myObj[a] = 'whatever';
alert(myObj.string1)
(alerts "whatever")
(提醒“随便”)
回答by user286806
Ecu, if you do myObj.a, then it looks for the property named a of myObj.
If you do myObj[a] =bthen it looks for the a.valueOf()property of myObj.
Ecu,如果你这样做了myObj.a,那么它会寻找名为 a 的 myObj 的属性。如果这样做,myObj[a] =b它会查找a.valueOf()myObj的属性。
回答by user2846569
Oneliner:
单线:
obj = (function(attr, val){ var a = {}; a[attr]=val; return a; })('hash', 5);
Or:
或者:
attr = 'hash';
val = 5;
var obj = (obj={}, obj[attr]=val, obj);
Anything shorter?
有更短的吗?
回答by Aaron George
You could just use this:
你可以用这个:
function createObject(propName, propValue){
this[propName] = propValue;
}
var myObj1 = new createObject('string1','string2');
Anything you pass as the first parameter will be the property name, and the second parameter is the property value.
您作为第一个参数传递的任何内容都将是属性名称,第二个参数是属性值。
回答by jroi_web
You cannot use a variable to access a property via dot notation, instead use the array notation.
您不能使用变量通过点表示法访问属性,而是使用数组表示法。
var obj= {
'name' : 'jroi'
};
var a = 'name';
alert(obj.a); //will not work
alert(obj[a]); //should work and alert jroi'
回答by Tuan
As $scope is an object, you can try with JavaScript by:
由于 $scope 是一个对象,您可以通过以下方式尝试使用 JavaScript:
$scope['something'] = 'hey'
It is equal to:
它等于:
$scope.something = 'hey'
回答by Trent
The following demonstrates an alternative approach for returning a key pair object using the form of (a, b). The first example uses the string 'key'as the property name, and 'val'as the value.
下面演示了使用 的形式返回密钥对对象的另一种方法(a, b)。第一个示例使用字符串'key'作为属性名称和'val'值。
Example #1:
示例#1:
(function(o,a,b){return o[a]=b,o})({},'key','val');
Example: #2:
示例:#2:
var obj = { foo: 'bar' };
(function(o,a,b){return o[a]=b,o})(obj,'key','val');
As shown in the second example, this can modify existing objects, too (if property is already defined in the object, value will be overwritten).
如第二个示例所示,这也可以修改现有对象(如果对象中已经定义了属性,则值将被覆盖)。
Result #1:
{ key: 'val' }Result #2:
{ foo: 'bar', key: 'val' }
结果#1:
{ key: 'val' }结果#2:
{ foo: 'bar', key: 'val' }

