从简单对象创建javascript对象 [英] Creating javascript object from simple object
本文介绍了从简单对象创建javascript对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有这个对象
var io={"NAME":"Battery For Alarm Panel","CODE":"POWER MAX","OWN":"ONM"}
我可以像下面那样访问
io['NAME'] or io['CODE'] etc.
但是如果想创建另一个对象,那么我如何访问 obj
就像下面的代码不工作,错误 Uncaught SyntaxError: Unexpected token [
But if want to create another object then how i can access obj
like below code is not working with error Uncaught SyntaxError: Unexpected token [
detailObj=
{
io['NAME']:
{
io['CODE']:
{
io['OWN']:"12"
}
}
}
我需要在 io
对象中进行哪些更改以创建 detailObj
What changes i need to made in io
object to create detailObj
推荐答案
你不能像这样声明一个对象,在属性名称的对象声明中使用变量.您必须像这样创建对象:
You can't declare an object like that, using variables inside of the object declaration for the property names. You'll have to create the object like this:
detailObj = {};
detailObj[io['NAME']] = {};
detailObj[io['NAME']][io['CODE']] = {};
detailObj[io['NAME']][io['CODE']][io['OWN']] = "12";
这篇关于从简单对象创建javascript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文