您将如何按距离对包含这些对象的数组进行排序。所以你有从最小距离到最大距离排序的对象?

Object { distance=3388, duration="6 mins", from="Lenchen Ave, Centurion 0046, South Africa", more...}

Object { distance=13564, duration="12 mins", from="Lenchen Ave, Centurion 0046, South Africa", more...}

Object { distance=4046, duration="6 mins", from="Lenchen Ave, Centurion 0046, South Africa", more...}

Object { distance=11970, duration="17 mins", from="Lenchen Ave, Centurion 0046, South Africa", more...}

最佳答案

使用 Array 's sort() method ,例如

myArray.sort(function(a, b) {
    return a.distance - b.distance;
});

关于javascript - 按对象键值对javascript中的数组进行排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7889006/

10-14 18:20