如何使用键值日期作为键对象进行排序

时间:2016-04-04 09:50:33

标签: javascript sorting object

我有以下对象,我想排序。关键是Unix中的日期字段,我需要保留每个键的值。我看到的例子摆脱了价值,但却对关键字进行排序。

{
"1458345600000":598.15,
"1458432000000":498.77000000000004,
"1458604800000":598.4699999999999,
"1458691200000":598.2900000000001,
"1458777600000":598.26,
"1458864000000":697.79,
"1459206000000":598.21,
"1459292400000":598.2800000000001,
"1459638000000":598.74,
"1457481600000":566.88,
"1457568000000":466.02000000000004,
"1457654400000":470.36,
"1457740800000":481.25,
"1457827200000":484.74000000000007,
"1457913600000":575.8199999999999,
"1458000000000":498.83000000000004,
"1458086400000":498.76,
"1458172800000":598.73,
"1458259200000":598.93,
"1458518400000":499.03999999999996,
"1458950400000":498.14,
"1459036800000":498.78,
"1459119600000":498.65999999999997,
"1459378800000":598.66,
"1459465200000":598.95,
"1459551600000":497.05
}

键是整数值;

更新

我尝试了以下仍无效的

var map = arr_cssr[key];
        console.log(JSON.stringify(map));
        Object.keys(map).sort((a,b) => parseInt(a) > parseInt(b));
        console.log(JSON.stringify(map));
        var subarr = [];
        for ( var k in map) {
            console.log(moment.unix(parseInt(k)/1000).format('YYYY-MM-DD')); 
            subarr.push({
                0 : parseInt(k),
                1 : map[k]/count
            });
        }

结果

{"1458345600000":595,"1458432000000":496,"1458604800000":595,"1458691200000":595,"1458777600000":595,"1458864000000":695,"1459206000000":596,"1459292400000":595,"1459638000000":597,"1459724400000":595,"1459810800000":695,"1457481600000":566,"1457568000000":463,"1457654400000":468,"1457740800000":480,"1457827200000":483,"1457913600000":574,"1458000000000":496,"1458086400000":496,"1458172800000":596,"1458259200000":596,"1458518400000":495,"1458950400000":495,"1459036800000":496,"1459119600000":496,"1459378800000":595,"1459465200000":596,"1459551600000":494}
{"1458345600000":595,"1458432000000":496,"1458604800000":595,"1458691200000":595,"1458777600000":595,"1458864000000":695,"1459206000000":596,"1459292400000":595,"1459638000000":597,"1459724400000":595,"1459810800000":695,"1457481600000":566,"1457568000000":463,"1457654400000":468,"1457740800000":480,"1457827200000":483,"1457913600000":574,"1458000000000":496,"1458086400000":496,"1458172800000":596,"1458259200000":596,"1458518400000":495,"1458950400000":495,"1459036800000":496,"1459119600000":496,"1459378800000":595,"1459465200000":596,"1459551600000":494}
2016-03-19
2016-03-20
2016-03-22
2016-03-23
2016-03-24
2016-03-25
2016-03-29
2016-03-30
2016-04-03
2016-04-04
2016-04-05
2016-03-09
2016-03-10
2016-03-11
2016-03-12
2016-03-13
2016-03-14
2016-03-15
2016-03-16
2016-03-17
2016-03-18
2016-03-21
2016-03-26
2016-03-27
2016-03-28
2016-03-31
2016-04-01
2016-04-02

5 个答案:

答案 0 :(得分:1)

我认为对具有键值对的Javascript对象进行排序没有任何意义。虽然在数组中它确实有意义,因为你将改变元素的索引,但在这个对象中我们不希望这样。

如果你想要按键排序的所有值,你仍然可以使用这样的东西:

var data = {
  "1458345600000": 598.15,
  "1458432000000": 498.77000000000004,
  "1458604800000": 598.4699999999999,
  "1458691200000": 598.2900000000001,
  "1458777600000": 598.26,
  "1458864000000": 697.79,
  "1459206000000": 598.21,
  "1459292400000": 598.2800000000001,
  "1459638000000": 598.74,
  "1457481600000": 566.88,
  "1457568000000": 466.02000000000004,
  "1457654400000": 470.36,
  "1457740800000": 481.25,
  "1457827200000": 484.74000000000007,
  "1457913600000": 575.8199999999999,
  "1458000000000": 498.83000000000004,
  "1458086400000": 498.76,
  "1458172800000": 598.73,
  "1458259200000": 598.93,
  "1458518400000": 499.03999999999996,
  "1458950400000": 498.14,
  "1459036800000": 498.78,
  "1459119600000": 498.65999999999997,
  "1459378800000": 598.66,
  "1459465200000": 598.95,
  "1459551600000": 497.05
};

var valuesSorted = Object.keys(data).sort().map(function(el) {
  return data[el];
});

console.log(valuesSorted);

现在valuesSorted将是具有按各自keys排序的所有值的数组。虽然请考虑根据需要更改排序比较功能。

答案 1 :(得分:1)

var map = { };

map = Object.keys(map);

map.sort(function(x, y) {
 if (x > y) return -1;
 if (x < y) return 1;
 if (x == y) return 0;
});

答案 2 :(得分:0)

您无法对对象进行排序。这是无序的。您可以改为使用Map,或者只是对其键进行排序。

Object.keys(obj).sort((a,b) => a > b);

答案 3 :(得分:0)

var a = { ... };
var sorted = Object.keys(a).map(k=>parseInt(k)).sort((a,b)=>a-b).map(k=>a[k.toString()]);

带有显式类型转换的数字排序。

答案 4 :(得分:0)

我必须对键进行排序,然后从原始对象中获取value

var map = arr_cssr[key];
        map = Object.keys(map);
        map.sort(function(x, y) {
         if (x > y) return -1;
         if (x < y) return 1;
         if (x == y) return 0;
        });
        var subarr = [];
        for (var i = 0; i < map.length; i++) {
            subarr.push({
                0 : parseInt(map[i]),
                1 : arr_cssr[key][map[i]]/count
            });
        }