在 Javascript 中为 JSON 对象赋值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8965135/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:13:50  来源:igfitidea点击:

Assigning Values to JSON Objects in Javascript

javascriptjsonobjectassign

提问by phpKid

This is what I'm trying to build via JavaScript in dot or [ ] notation:

这就是我试图通过 JavaScript 以点或 [] 表示法构建的内容:

var shoppingCart = { 
        'item1' : {
            'description' : 'This is item #1',
            'price' : 10,
            'quantity' : 1,
            'shipping' : 0,
            'total' : 10
        }
    };

Now if 'item1' is the variable name itemName.

现在,如果 'item1' 是变量 name itemName

This works:
var shoppingCart = {};
shoppingCart[itemName] = itemName;
alert(shoppingCart.item1);

这有效:
var shoppingCart = {};
shoppingCart[itemName] = itemName;
alert(shoppingCart.item1);

Which returns item1

哪个返回 item1

But this doesn't work:
1 var shoppingCart = {};
2 shoppingCart[itemName]['description'] = 'This is Item #1';

但这不起作用:
1 var shoppingCart = {};
2 shoppingCart[itemName]['description'] = 'This is Item #1';

JS just dies at line 2, why? and how do I assign the description value to 'description'?

JS 就死在第 2 行了,为什么?以及如何将描述值分配给“描述”?

I would do it like this:

我会这样做:

var shoppingCart = { 
        itemName : {
            'description' : description,
            'price' : price,
            'quantity' : quantity,
            'shipping' : shipping,
            'total' : total
        }
    };

...but it makes the key literally itemNameinstead of item1.

...但它使密钥字面上itemName而不是item1.

回答by SLaks

shoppingCart[itemName]doesn't exist.
You need to create it first:

shoppingCart[itemName]不存在。
您需要先创建它:

var shoppingCart = {};
shoppingCart[itemName] = { };
shoppingCart[itemName].description = 'This is Item #1';

Or, better yet:

或者,更好的是:

var shoppingCart = {};
shoppingCart[itemName] = { description: 'This is Item #1' };

回答by 6502

In javascript objects do not auto-create on member access; you have to create the object first:

在 javascript 中,对象不会在成员访问时自动创建;你必须先创建对象:

var shoppingCart = {};
shoppingCart["item1"] = {}; // This creates the object
shoppingCart["item1"]["description"] = "This is item #1"; // add a member

Note you can of course also create the whole object at once with

请注意,您当然也可以使用

shoppingCart[itemname] = { "description": description,
                           "price": price,
                           "quantity": quantity,
                           "shipping": shipping,
                           "total": total };

回答by Ryan McGrath

It doesn't work because, in your second example, you're trying to index a blank object with a key that hasn't been set. The following will work...

它不起作用,因为在您的第二个示例中,您尝试使用尚未设置的键为空白对象建立索引。以下将工作...

var shoppingCart = {};
shoppingCart[itemName] = {};
shoppingCart[itemName]['description'] = '...';

Because now shoppingCart[itemName] is actually an object.

因为现在shoppingCart[itemName] 实际上是一个对象。

回答by Renat Gatin

Just FYI, alternative solution:

仅供参考,替代解决方案:

var itemName = {};
itemName.description = 'This is Item #1';
var shoppingCart = {};
shoppingCart.itemName = itemName;

or just

要不就

var shoppingCart = {};
shoppingCart.itemName = { description: 'This is Item #1' };