我想知道如何从状态' 0'中获取坐标。或者' EMPTY'在给出的所有数据中。
var locations = [
['FULL',3.720185, 103.124075,1],
['EMPTY',3.719693, 103.123896,0],
['FULL',3.720916, 103.12492,1],
['EMPTY',3.721032, 103.124532,0],
['FULL',3.722299, 103.124587,1],
['FULL',3.723189, 103.124706,1],
['FULL',3.725067, 103.124593,1]
];
for (var k=0;k<locations.length; k++)
{
if (locations[i][3]==0) {
var diff=getDistanceFromLatLonInKm(position.coords.latitude,position.coords.longitude,locations[k][1],locations[k][2]);
x.innerHTML = "<h1>Distance of nearest bin: "+ Math.min(diff)+"km</h1>";
}
这是我到目前为止所尝试的。
答案 0 :(得分:0)
使用Array#filter
,传递谓词函数,该函数检查每个位置数组是否具有等于EMPTY
的第一个元素或等于0
的第四个元素(falsy) :
var locations = [
['FULL', 3.720185, 103.124075, 1],
['EMPTY', 3.719693, 103.123896, 0],
['FULL', 3.720916, 103.12492, 1],
['EMPTY', 3.721032, 103.124532, 0],
['FULL', 3.722299, 103.124587, 1],
['FULL', 3.723189, 103.124706, 1],
['FULL', 3.725067, 103.124593, 1]
]
var result = locations.filter(function (e) {
return e[0] === 'EMPTY' || !e[3]
})
console.log(result)
.as-console-wrapper { min-height: 100%; }
答案 1 :(得分:0)
你可以filter
地点:
var locations = [
['FULL', 3.720185, 103.124075, 1],
['EMPTY', 3.719693, 103.123896, 0],
['FULL', 3.720916, 103.12492, 1],
['EMPTY', 3.721032, 103.124532, 0],
['FULL', 3.722299, 103.124587, 1],
['FULL', 3.723189, 103.124706, 1],
['FULL', 3.725067, 103.124593, 1]
];
var emptyLocations = locations.filter(function(location) {
return location[0] === 'EMPTY' || location[3] === 0;
});
console.log(emptyLocations);
// [["EMPTY", 3.719693, 103.123896, 0], ["EMPTY", 3.721032, 103.124532, 0]]