javascript 通过使用现有元素创建新字段来修改 JSON 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7540071/
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
Modifying a JSON object by creating a New Field using existing Elements
提问by Question Overflow
I have a javascript object that goes like this:
我有一个像这样的 javascript 对象:
var obj = {"data":
[
{"name":"Alan","height":1.71,"weight":66},
{"name":"Ben","height":1.82,"weight":90},
{"name":"Chris","height":1.63,"weight":71}
]
,"school":"Dover Secondary"
}
How do I create a new field called BMI using weight/(height)^2 such that the new object becomes:
如何使用 weight/(height)^2 创建一个名为 BMI 的新字段,以便新对象变为:
var new_obj = {"data":
[
{"name":"Alan","height":1.71,"weight":66,"BMI":22.6},
{"name":"Ben","height":1.82,"weight":90,"BMI":27.2},
{"name":"Chris","height":1.63,"weight":71,"BMI":26.7}
]
,"school":"Dover Secondary"
}
回答by Rob W
var persons = obj.data;
var new_obj = {data: [], school: obj.school};
for(var i=0; i<persons.length; i++){
var person = persons[i];
new_obj.data.push({
name: person.name,
height: person.height,
weight: person.weight,
BMI: Math.round(person.weight / Math.pow(person.height, 2)*10)/10;
});
/* Use the next line if you don't want to create a new object,
but extend the current object:*/
//persons.BMI = Math.round(person.weight / Math.pow(person.height, 2)*10)/10;
}
After new_obj
is initialised, a loop walks through array obj.data
. The BMI is calculated, and added along with a copy of all properties to new_obj
. If you don't have to copy the object, have a look at the commented part of the code.
之后new_obj
被初始化,循环走过阵列obj.data
。计算 BMI,并与所有属性的副本一起添加到new_obj
. 如果您不必复制对象,请查看代码的注释部分。
回答by Naga Harish M
Try with this code, in this below code I used same object to add one more field. We can also have copy of the original object by copying existing one to temp variable
尝试使用此代码,在下面的代码中,我使用相同的对象再添加一个字段。我们还可以通过将现有对象复制到临时变量来获得原始对象的副本
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Modifying a JSON object by creating a New Field using existing Elements</title>
</head>
<body>
<h2>Modifying a JSON object by creating a New Field using existing Elements</h2>
<script type="text/javascript">
var obj = { "data":
[
{ "name": "Alan", "height": 1.71, "weight": 66 },
{ "name": "Ben", "height": 1.82, "weight": 90 },
{ "name": "Chris", "height": 1.63, "weight": 71 }
]
, "school": "Dover Secondary"
}
alert(obj.data[0].weight);
var temp=obj["data"];
for (var x in temp) {
var w=temp[x]["weight"];
var h=temp[x]["height"];
temp[x]["BMI"] = (w / (h) ^ 2) ;
}
alert(obj.data[1].BMI);
</script>
</body>
</html>
回答by aziz punjani
var data = obj['data'];
for( var i in data )
{
var person = data[i];
person.BMI = (person.weight/ Math.pow(person.height, 2)).toFixed(2) ;
}