如何根据状态[0,1]对某些坐标进行排序

时间:2017-03-28 16:52:31

标签: javascript arrays sorting

我想知道如何从状态' 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>";
        }

这是我到目前为止所尝试的。

2 个答案:

答案 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]]