let array=[
{
"id": "a572963a1bcd0550c4cf8518624bcb92",
"type": "car",
"created": "2022-01-18 12:47:08"
},
{
"id": "b84eb0fe1bc10550c4cf8518624bcb59",
"type": "car",
"created": "2022-01-18 06:23:39"
},
{
"id": "a95b65fc1b898110c4cf8518624bcbee",
"type": "bike",
"created": "2022-01-12 04:30:16"
},
{
"id": "5963929c1b018dd0c4cf8518624bcbc9",
"type": "bike",
"created": "2022-01-10 18:08:19"
},
{
"id": "515b65fc1b898110c4cf8518624bcb91",
"type": "bus",
"created": "2022-01-12 04:30:16"
},
{
"id": "7863929c1b018dd0c4cf8518624bcb52",
"type": "bus",
"created": "2022-01-10 18:08:17"
}
]
我想根據創建的 JSON 中可用的日期對上述 JSON 陣列中的資料進行排序。僅選擇最新創建的資料。
uj5u.com熱心網友回復:
array.sort((a,b) => a.created > b.created ? 1 : -1)
如果創建的日期字串格式相同,這應該可以作業
uj5u.com熱心網友回復:
按日期屬性對專案進行排序:信用
array.sort(function(a,b){
// Turn your strings into dates, and then subtract them
// to get a value that is either negative, positive, or zero.
return new Date(b.date) - new Date(a.date);
});
然后:
array.shift();
uj5u.com熱心網友回復:
您可以使用排序功能根據日期值對其進行排序。一個班輪代碼將如下所示
let sortedArray = array.sort((a, b)=> (new Date(b.created) - new Date(a.created)));
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/414909.html
標籤:
