javascript 如何附加到空的 JSON 对象?

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

How can I append to an empty JSON object?

javascriptjson

提问by Shamoon

  var kids = [{}];
  i = 0;
  while (i++ !== count) {
    child = {
      value: Math.floor(Math.random() * 100),
      children: addChildren(Math.floor(Math.random() * 10))
    };
    kids.push(child);
    console.log( kids );
  }

The problem with this is the kidsobject has an empty first element. How can I circumvent it? If I don't declare it as a JSON object, I can't access the pushelement.

问题是该kids对象有一个空的第一个元素。我怎样才能规避它?如果我不将其声明为 JSON 对象,则无法访问该push元素。

Thanks

谢谢

回答by Chris Pietschmann

Just declare kids as an empty array:

只需将 kids 声明为空数组:

var kids = [];

回答by Ray Toal

It sounds like you want to end up with a JavaScript object containing several instances of objects that have two keys valueand children. It would seem an array is the best choice (which Khnle's and Chris's answers give you):

听起来您希望最终得到一个 JavaScript 对象,该对象包含多个具有两个键value和的对象实例children。数组似乎是最好的选择(Khnle 和 Chris 的答案给了你):

[{"value":2,"children":3}, {"value":12,"children":9}, {"value":20,"children":13}]

In your comment to one of the answers, however, you said that you did not want an array. One way to do this is to wrap it, as in Jergason's answer:

但是,在您对其中一个答案的评论中,您说您不想要数组。一种方法是包装它,如 Jergason 的回答:

{
    "children": [
        {"value":2,"children":3}, 
        {"value":12,"children":9}, 
        {"value":20,"children":13}
    ]
}

Your question seemed to say that you like arrays because you get the pushoperation, but you would like to avoid them completely. The only way to avoid arrays completelyis to tag each object with its own unique key. If indeed this is what you want, it would look like this:

你的问题似乎是说你喜欢数组是因为你得到了push操作,但你想完全避免它们。完全避免数组的唯一方法是用自己唯一的键标记每个对象。如果这确实是您想要的,它看起来像这样:

{
    "child0":{"value":2,"children":3}, 
    "child1":{"value":12,"children":9}, 
    "child2":{"value":20,"children":13}
}

This is not hard to do; just replace kids.push(child)with kids["child" + i] = child.

这并不难做到;只需替换kids.push(child)kids["child" + i] = child.

Make sure this is really what you want, though, because this collection of children really seems to scream "array"! :-)

不过,请确保这确实是您想要的,因为这组孩子似乎真的在尖叫“阵列”!:-)

回答by jergason

You could just have your object contain an empty array.

你可以让你的对象包含一个空数组。

var obj = { "children": [] };
// Your looping code here
obj.children.push(child);

回答by Kevin Le - Khnle

With some slight changes (add var for local variable):

有一些细微的变化(为局部变量添加 var):

var kids = []; //Note this line
var i = 0; //index counter most likely needs to be local
while (i++ !== count) {
    var child = {
       value: Math.floor(Math.random() * 100),
       children: addChildren(Math.floor(Math.random() * 10))
    };
    kids.push(child);
    console.log(kids);
 }

I think that's what you would want.

我想这就是你想要的。

Update: Your requirement is rather odd. You can do this:

更新:您的要求很奇怪。你可以这样做:

 var kids = [{}]; //Note this line
 delete kids[0];
 var i = 0; //index counter most likely needs to be local
 while (i++ !== count) {
    var child = {
       value: Math.floor(Math.random() * 100),
       children: addChildren(Math.floor(Math.random() * 10))
    };
    kids.push(child);
    console.log(kids);
 }

or

或者

 var kids = [{
       value: Math.floor(Math.random() * 100),
       children: addChildren(Math.floor(Math.random() * 10))
 }];
 var i = 1; //1 less iteration than previous
 while (i++ !== count) {
    var child = {
       value: Math.floor(Math.random() * 100),
       children: addChildren(Math.floor(Math.random() * 10))
    };
    kids.push(child);
    console.log(kids);
 }

That would satisfy your requirement, but what I show originally is still, I think, what you want.

那会满足你的要求,但我原来展示的,我想,还是你想要的。