与数组作为价值排序的JavaScript对象 [英] Sort javascript object with array as value
问题描述
我在格式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]}$c$c>
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]}$c$c>
显然排序()
功能将开始发挥作用,这里的阵列,但我如何访问对象内部的价值观?注意对象键
实际上不是一个整数,因为下划线。
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屋!