这里我有一个nArray
函数,它返回一个bigArray
- JSON所以使用nArray函数我创建一个bigArray对象
示例:
Array[5]
0: Object
1: Object
DISTANCE_FROM_PREVIOUS_OBJECT_LOCATION: 2.087970147789207
lat: "48.866588"
leftPosition: 183
lng: "2.309037999999987"
topPosition: 57
__proto__: Object
2: Object
3: Object
4: Object
length: 5
__proto__: Array[0]
正如你所看到的,我在bigArray中有Objetcs,包含:lat,left,lng,top和DISTANCE _
现在我需要使用函数updateTooltipContent
从OBJECT获取DISTANCE,其中是latt = lat:
function updateTooltipContent() {
var latt = $(this).attr("lat");
//FIND IN WHICH OBJECT IS latt = lat and from that OBJECT[i] get DISTANCE_ ...
return "Distance is" + DISTANCE_FROM_PREVIOUS_LOCATION;
}
});
有没有办法做到这一点?所以我需要调用函数nArray
来创建bigArray并从中找到objet是latt = lat并从该对象获取[i] DISTANCE
答案 0 :(得分:1)
当nArray
函数返回bigArray时,您只需要循环遍历它,并在每次迭代时检查latt == lat
。
var bigArray = nArray();
var distance = 0.00;
for(var x = 0; x < bigArray.length; x++)
{
if(bigArray[x].lat == latt)
{
distance = bigArray[x].DISTANCE_FROM_PREVIOUS_OBJECT_LOCATION;
break; // no point doing anymore loops as we've found the answer
}
}
可能需要在js中使用parseFloat函数,具体取决于您处理数字的方式。