与数组作为价值排序的JavaScript对象 [英] Sort javascript object with array as value

查看:116
本文介绍了与数组作为价值排序的JavaScript对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在格式Javascript对象键:对其中是包含2时间戳值的数组。我想这个排序的对象,以便用最低的数字(最早的时候)的元素首先显示出来。

I have a Javascript Object in the format key:pair where the pair is an array containing 2 timestamp values. I would like to sort this object so that the elements with the lowest numbers (earliest times) are displayed first.

下面是对象的一个​​例子: <$c$c>{\"21_2\":[1409158800,1409160000],\"20_1\":[1409148000,1409149200],\"56_1\":[1409149800,1409151600]}

Here is an example of the object: {"21_2":[1409158800,1409160000],"20_1":[1409148000,1409149200],"56_1":[1409149800,1409151600]}

因此​​,在这种情况下,我想最终排序的对象读取:

So in this case, I would want the final sorted object to read:

<$c$c>{\"20_1\":[1409148000,1409149200],\"56_1\":[1409149800,1409151600],\"21_2\":[1409158800,1409160000]}

显然排序()功能将开始发挥作用,这里的阵列,但我如何访问对象内部的价值观?注意对象实际上不是一个整数,因为下划线。

Obviously the sort() function will come into play here for the arrays, but how do I access those values inside the object? Note the object key isn't actually an integer because of the underscore.

我发现这个:数组内的排序一系列复杂通过值但一直无法将它应用到我的情况。任何帮助将大大AP preciated。

I have found this: Sort Complex Array of Arrays by value within but haven't been able to apply it to my situation. Any help would be greatly appreciated.

推荐答案

您可以改变你的数据的结构是这样的:

You could change the structure of your data like this:

var myArray = [{
        "id" : "21_2" // id or whatever these are
        "timeStamps" : [1409158800,1409160000]
    }, {
        "id" : "20_1"
        "timeStamps" : [1409148000,1409149200]
    }];

然后,你可以通过常规的数组排序排序是:

Then you could sort it by the regular array sort:

myArray.sort(function(a, b){
    // however you want to compare them:
    return a.timeStamps[0] - b.timeStamps[0]; 
});

这篇关于与数组作为价值排序的JavaScript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆