如何附加到空的JSON对象?

时间:2023-01-24 09:13:53
  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 kids object 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 push element.

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

Thanks

4 个解决方案

#1


9  

Just declare kids as an empty array:

只需将孩子声明为空数组:

var kids = [];

#2


3  

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

听起来你想要最终得到一个JavaScript对象,其中包含几个具有两个键值和子项的对象实例。看起来阵列是最好的选择(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 push operation, but you would like to avoid them completely. The only way to avoid arrays completely is to tag each object with its own unique key. If indeed this is what you want, it would look like this:

您的问题似乎表明您喜欢数组,因为您获得了推送操作,但您希望完全避免它们。完全避免数组的唯一方法是使用自己的唯一键标记每个对象。如果这确实是你想要的,它将如下所示:

{
    "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(孩子)替换为孩子[“孩子”+ i] =孩子。

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

确保这真的是你想要的,因为这个孩子的集合真的似乎尖叫“阵列”! :-)

#3


1  

You could just have your object contain an empty array.

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

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

#4


0  

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.

这样可以满足你的要求,但我想,我想要的仍然是你想要的。

#1


9  

Just declare kids as an empty array:

只需将孩子声明为空数组:

var kids = [];

#2


3  

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

听起来你想要最终得到一个JavaScript对象,其中包含几个具有两个键值和子项的对象实例。看起来阵列是最好的选择(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 push operation, but you would like to avoid them completely. The only way to avoid arrays completely is to tag each object with its own unique key. If indeed this is what you want, it would look like this:

您的问题似乎表明您喜欢数组,因为您获得了推送操作,但您希望完全避免它们。完全避免数组的唯一方法是使用自己的唯一键标记每个对象。如果这确实是你想要的,它将如下所示:

{
    "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(孩子)替换为孩子[“孩子”+ i] =孩子。

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

确保这真的是你想要的,因为这个孩子的集合真的似乎尖叫“阵列”! :-)

#3


1  

You could just have your object contain an empty array.

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

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

#4


0  

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.

这样可以满足你的要求,但我想,我想要的仍然是你想要的。