如何动态设置 Javascript 对象值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6439915/
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 Javascript object values dynamically?
提问by Umut KIRG?Z
It's difficult to explain the case by words, let me give an example:
一言难尽,举个例子:
var myObj = {
'name': 'Umut',
'age' : 34
};
var prop = 'name';
var value = 'Onur';
myObj[name] = value; // This does not work
eval('myObj.' + name) = value; //Bad coding ;)
How can I set a variable property with variable value in a JavaScript object?
如何在 JavaScript 对象中设置具有变量值的变量属性?
回答by Matt Greer
myObj[prop] = value;
That should work. You mixed up the name of the variable and its value. But indexing an object with strings to get at its properties works fine in JavaScript.
那应该工作。您混淆了变量的名称及其值。但是用字符串索引对象以获取其属性在 JavaScript 中工作正常。
回答by bcoughlan
myObj.name=value
or
或者
myObj['name']=value (Quotes are required)
Both of these are interchangeable.
这两者是可以互换的。
Edit:I'm guessing you meant myObj[prop] = value
, instead of myObj[name] = value. Second syntax works fine: http://jsfiddle.net/waitinforatrain/dNjvb/1/
编辑:我猜你的意思是myObj[prop] = value
,而不是 myObj[name] = value。第二种语法工作正常:http: //jsfiddle.net/waitinoratrain/dNjvb/1/
回答by timw4mail
You can get the property the same way as you set it.
您可以通过与设置属性相同的方式获取该属性。
foo = {
bar: "value"
}
You set the value
foo["bar"] = "baz";
你设置的值
foo["bar"] = "baz";
To get the value
foo["bar"]
获取价值
foo["bar"]
will return "baz".
将返回“baz”。
回答by timw4mail
You could also create something that would be similar to a value object (vo);
您还可以创建类似于值对象 (vo) 的东西;
SomeModelClassNameVO.js;
SomeModelClassNameVO.js;
function SomeModelClassNameVO(name,id) {
this.name = name;
this.id = id;
}
Than you can just do;
比你能做的更多;
var someModelClassNameVO = new someModelClassNameVO('name',1);
console.log(someModelClassNameVO.name);
回答by NT3RP
When you create an object myObj
as you have, think of it more like a dictionary. In this case, it has two keys, name
, and age
.
当你创建一个对象时myObj
,把它想象成一本字典。在这种情况下,它有两个键name
, 和age
。
You can access these dictionaries in two ways:
您可以通过两种方式访问这些词典:
- Like an array (e.g.
myObj[name]
); or - Like a property (e.g.
myObj.name
); do note that some properties are reserved, so the first method is preferred.
- 像一个数组(例如
myObj[name]
);或者 - 像一个财产(例如
myObj.name
);请注意,某些属性是保留的,因此首选第一种方法。
You should be able to access it as a property without any problems. However, to access it as an array, you'll need to treat the key like a string.
您应该能够毫无问题地将其作为属性访问。但是,要将其作为数组访问,您需要将键视为字符串。
myObj["name"]
Otherwise, javascript will assume that name
is a variable, and since you haven't created a variable called name
, it won't be able to access the key you're expecting.
否则,javascript 将假定这name
是一个变量,并且由于您尚未创建名为 的变量name
,因此它将无法访问您期望的密钥。
回答by Arturo Martinez
simple as this
myObj.name = value;
就这么简单
myObj.name = value;