将新属性动态添加到Node中的现有JSON数组

时间:2020-12-17 23:14:19

I need to add an attribute which doesn't exist in the current JSON. The json object looks like below.

我需要添加当前JSON中不存在的属性。 json对象如下所示。

var jsonObj = {
        "result" : "OK",
        "data" : []
    };

And I want to add temperature inside 'data'. I could do it like below.

我想在'数据'中添加温度。我可以像下面这样做。

jsonObj.data.push( {temperature : {}} );

And then, I want to add 'home', 'work' inside 'temperature'. The result would be like below.

然后,我想在'温度'内添加'home','work'。结果如下。

{
    "result" : "OK",
    "data" : [
        { "temperature" : {
            "home" : 24,
            "work" : 20 } }
    ]
};

How could I do this? I succeeded to insert 'temperature' inside 'data', but couldn't add 'home' & 'work' inside temperature. There could be more inside the 'temperature' so it needs to be enclosed with {}.

我怎么能这样做?我成功地在'数据'中插入'温度',但无法在温度范围内添加'home'和'work'。 '温度'内可能有更多,所以需要用{}括起来。

2 个解决方案

#1


13  

How about this?

这个怎么样?

var temperature = { temperature: { home: 24, work: 20 }};

jsonObj.data.push(temperature);

I can't tell if doing it in two steps is important by how your question is structured, but you could add the home and work properties later by indexing into the array, like this:

我不知道在两个步骤中执行它对于您的问题的结构是如何重要的,但您可以稍后通过索引到数组中添加home和work属性,如下所示:

jsonObj.data.push({ temperature: { }});

jsonObj.data[0].temperature.home = 24;
jsonObj.data[0].temperature.work = 20;

But, that's not a great idea since it depends on the array index to find the temperature object. If that's a requirement, it would be better to do something like loop through the data array to find the object you're interested in conclusively.

但是,这不是一个好主意,因为它依赖于数组索引来查找温度对象。如果这是一个要求,最好做一些事情,比如循环数据数组,以确定地找到你感兴趣的对象。

Edit:

An example of looping through to locate the temperature object would go something like this:

循环查找温度对象的示例将如下所示:

for (var i = 0; i < jsonObj.data.length; i++) { 
  if (jsonObj.data[i].temperature) { 
    break;
  }
}

jsonObj.data[i].temperature.home = 24;
jsonObj.data[i].temperature.work = 20;

Edit:

If which particular property you'll be interested in is unknown at development time, you can use bracket syntax to vary that:

如果您在开发时对哪个特定属性感兴趣,则可以使用括号语法来改变:

for (var i = 0; i < jsonObj.data.length; i++) { 
  if (jsonObj.data[i]['temperature']) { 
    break;
  }
}

jsonObj.data[i]['temperature'].home = 24;
jsonObj.data[i]['temperature'].work = 20;    

Which means you could use a variable there instead of hard coding it:

这意味着您可以在那里使用变量而不是硬编码:

var target = 'temperature';

for (var i = 0; i < jsonObj.data.length; i++) { 
  if (jsonObj.data[i][target]) { 
    break;
  }
}

jsonObj.data[i][target].home = 24;
jsonObj.data[i][target].work = 20;

#2


0  

How about

jsonObj.data.push( {
  temperature : {
        "home" : 24,
        "work" : 20
  } 
} );

#1


13  

How about this?

这个怎么样?

var temperature = { temperature: { home: 24, work: 20 }};

jsonObj.data.push(temperature);

I can't tell if doing it in two steps is important by how your question is structured, but you could add the home and work properties later by indexing into the array, like this:

我不知道在两个步骤中执行它对于您的问题的结构是如何重要的,但您可以稍后通过索引到数组中添加home和work属性,如下所示:

jsonObj.data.push({ temperature: { }});

jsonObj.data[0].temperature.home = 24;
jsonObj.data[0].temperature.work = 20;

But, that's not a great idea since it depends on the array index to find the temperature object. If that's a requirement, it would be better to do something like loop through the data array to find the object you're interested in conclusively.

但是,这不是一个好主意,因为它依赖于数组索引来查找温度对象。如果这是一个要求,最好做一些事情,比如循环数据数组,以确定地找到你感兴趣的对象。

Edit:

An example of looping through to locate the temperature object would go something like this:

循环查找温度对象的示例将如下所示:

for (var i = 0; i < jsonObj.data.length; i++) { 
  if (jsonObj.data[i].temperature) { 
    break;
  }
}

jsonObj.data[i].temperature.home = 24;
jsonObj.data[i].temperature.work = 20;

Edit:

If which particular property you'll be interested in is unknown at development time, you can use bracket syntax to vary that:

如果您在开发时对哪个特定属性感兴趣,则可以使用括号语法来改变:

for (var i = 0; i < jsonObj.data.length; i++) { 
  if (jsonObj.data[i]['temperature']) { 
    break;
  }
}

jsonObj.data[i]['temperature'].home = 24;
jsonObj.data[i]['temperature'].work = 20;    

Which means you could use a variable there instead of hard coding it:

这意味着您可以在那里使用变量而不是硬编码:

var target = 'temperature';

for (var i = 0; i < jsonObj.data.length; i++) { 
  if (jsonObj.data[i][target]) { 
    break;
  }
}

jsonObj.data[i][target].home = 24;
jsonObj.data[i][target].work = 20;

#2


0  

How about

jsonObj.data.push( {
  temperature : {
        "home" : 24,
        "work" : 20
  } 
} );