简体   繁体   English

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

[英]How to sort an object by date in keys in Javascript

I have the following object: 我有以下对象:

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 };

I would like to sort it by date, beginning from January, but I have no idea how to do this. 我想按日期排序,从一月开始,但是我不知道该怎么做。

What do you think, is it possible in JS ? 您如何看待,在JS中可能吗?

Thanks in advance, 提前致谢,

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);
}

This will work 这会起作用

Hope this helps..! 希望这可以帮助..!

The order of keys is not guaranteed in an object. 不能保证对象中键的顺序。 you can convert your data structure into a sorted array as shown below and iterate over the array in order for your needs: 您可以将数据结构转换为如下所示的排序数组,并在数组上进行迭代以满足您的需要:

 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 } ] **/ 

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

You can not order object proprieties. 您不能订购对象专有权。 so you may need to convert your object to an array 因此您可能需要将对象转换为数组

Example

 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); 

Thanks @Shivam for your note 感谢@Shivam给您的笔记

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM