我有一个具有这种结构的现有多维关联数组。它有3个元素。每个子阵列具有3个元素。
{"id":"723419","lat":33.217,"lon":-92.817,"dist":1437.15733534053},
{"id":"723424","lat":33.567,"lon":-91.717,"dist":1902.4556686060116},
{"id":"722447","lat":32.383,"lon":-94.717,"dist":2317.6870313059217},
我想用3个元素创建另一个(temp)数组,并循环遍历数组,为每个子数组添加一个元素,以便每个子数组都有4个元素:
{"id":"723419","lat":33.217,"lon":-92.817,"dist":1437.15733534053,"elev":abc},
{"id":"723424","lat":33.567,"lon":-91.717,"dist":1902.4556686060116,"elev":def},
{"id":"722447","lat":32.383,"lon":-94.717,"dist":2317.6870313059217,"elev":ghi},
到目前为止,我已经尝试遍历我的临时数组
for (var i in tempArray) { //loop through locations returned with elevation data
multiArray[i]['elev']=...
}
并将其视为对象(即使我将多数组声明为Array()
for (var i in tempArray) { //loop through locations returned with elevation data
multiArray.i.append(elev)=...
}
两种方式,JS都抱怨multiArray[i]
不存在。根据我对JS的了解,这应该有效。
感谢您的帮助
答案 0 :(得分:1)
我制作了具有预定义值的简单演示,然后创建另一个数组,从第一个数组中获取它的值,然后添加新元素elev
var arr = [{
"id": "723419",
"lat": 33.217,
"lon": -92.817,
"dist": 1437.15733534053
}, {
"id": "723424",
"lat": 33.567,
"lon": -91.717,
"dist": 1902.4556686060116
}, {
"id": "722447",
"lat": 32.383,
"lon": -94.717,
"dist": 2317.6870313059217
}];
var tempArray = new Array();
for (var item in arr) {
tempArray.push(arr[item]);
tempArray[item]["elev"] = "value"; //write here your value
}
<强> JSFIDDLE 强>