我正在尝试遍历JSON并按日期排序,以便查看最新日期至最旧日期,然后将其写入文件。
以下是我的代码:
以下是我的代码:
var reader = JSON.parse(fs.readFileSync('txt.json', 'utf8'));
function sortByDate(a, b) {
return new Date(a.lastUpdated).toJSON() - new Date(b.lastUpdated).toJSON();
}
reader.sort(sortByDate)
JSON数据示例
{
"Data": {
"Contents": [
{
"Key": [
"HelloTest"
],
"lastUpdated": [
"2019-10-25T10:30:50.558Z"
]
},
{
"Key": [
"TestHello"
],
"lastUpdated": [
"2019-03-26T10:30:50.558Z"
]
}
]
}
}
Key
andlastUpdated
are collections in your json so I think you should trya.lastUpdated[0]
andb.lastUpdated[0]
- Chetan