如何在Javascript中的键中按日期对对象进行排序

时间:2017-06-28 11:34:02

标签: javascript javascript-objects

我有以下对象:

var obj = { 2017-05-28: 76.16108625212159, 2017-02-12: 70.32183347555772, 2017-05-21: 74.21070693205216, 2017-04-23: 78.49819059107358, 2017-03-05: 73.36286201022146, 2017-04-02: 79.07588060050237, 2017-01-29: 79.07021235890568, 2017-03-26: 74.79360018220122, 2017-01-22: 71.80166785183269, 2017-04-09: 72.68569443640364 };

我想从1月开始按日期排序,但我不知道该怎么做。

你怎么想,JS有可能吗?

提前致谢,

3 个答案:

答案 0 :(得分:1)

var obj = '{ "2017-05-28": "76.16108625212159", "2017-02-12": "70.32183347555772", "2017-05-21": "74.21070693205216", "2017-04-23": "78.49819059107358", "2017-03-05": "73.36286201022146", "2017-04-02": "79.07588060050237", "2017-01-29": "79.07021235890568", "2017-03-26": "74.79360018220122", "2017-01-22": "71.80166785183269", "2017-04-09": "72.68569443640364" }';
pobject = JSON.parse(obj);
arr = Object.keys(pobject);
sorted = arr.sort();
len = sorted.length;
for(i=0;i<len;i++)
{
val= pobject[sorted[i]];
console.log(val);
}

这将有效

希望这会有所帮助..!

答案 1 :(得分:1)

对象中无法保证键的顺序。您可以将数据结构转换为排序数组,如下所示,并迭代数组以满足您的需求:

&#13;
&#13;
var obj = {
  "2017-05-28": 76.16108625212159,
  "2017-02-12": 70.32183347555772,
  "2017-05-21": 74.21070693205216,
  "2017-04-23": 78.49819059107358,
  "2017-03-05": 73.36286201022146,
  "2017-04-02": 79.07588060050237,
  "2017-01-29": 79.07021235890568,
  "2017-03-26": 74.79360018220122,
  "2017-01-22": 71.80166785183269,
  "2017-04-09": 72.68569443640364
};

var sortedArray = Object.keys(obj).sort().map(function(key) {
  return {
    date: key,
    value: obj[key]
  }
});

console.log(sortedArray);
/**Outputs:
[
  {
    "date": "2017-01-22",
    "value": 71.80166785183269
  },
  {
    "date": "2017-01-29",
    "value": 79.07021235890568
  },
  {
    "date": "2017-02-12",
    "value": 70.32183347555772
  },
  {
    "date": "2017-03-05",
    "value": 73.36286201022146
  },
  {
    "date": "2017-03-26",
    "value": 74.79360018220122
  },
  {
    "date": "2017-04-02",
    "value": 79.07588060050237
  },
  {
    "date": "2017-04-09",
    "value": 72.68569443640364
  },
  {
    "date": "2017-04-23",
    "value": 78.49819059107358
  },
  {
    "date": "2017-05-21",
    "value": 74.21070693205216
  },
  {
    "date": "2017-05-28",
    "value": 76.16108625212159
  }
]
**/
&#13;
&#13;
&#13;

答案 2 :(得分:0)

使用Array.prototype.sort()Array.prototype.reduce()

您无法订购对象礼仪。所以你可能需要将你的对象转换为数组

示例

var obj = {
        "2017-05-28": 76.16108625212159,
        "2017-02-12": 70.32183347555772,
        "2017-05-21": 74.21070693205216,
        "2017-04-23": 78.49819059107358,
        "2017-03-05": 73.36286201022146,
        "2017-04-02": 79.07588060050237,
        "2017-01-29": 79.07021235890568,
        "2017-03-26": 74.79360018220122,
        "2017-01-22": 71.80166785183269,
        "2017-04-09": 72.68569443640364
    },

    sortedArr = Object.keys(obj)
        .sort(function (a, b) { 
            return new Date(a) - new Date(b)
         })
        .reduce(function (acc, item, index) {
            acc.push({
                date: item,
                value: obj[item]
            });
            return acc;
        }, []);
    console.log(sortedArr);

感谢@Shivam的笔记