React 以 UTC 格式排序日期 (e:2019-11-14T10:18:51Z)
React Sorting Dates in UTC format (e:2019-11-14T10:18:51Z)
我确实有一个 json,它以 DateModified 作为键,它的值为 UTC 格式。
我需要一个排序切换按钮,以便在切换时它应该根据这个日期排序(最新的在第一位)
谁能帮我弄明白。
这是 myJSON
{
"jpg": [
{
"extension": "jpg",
"width": "450",
"caption": "first",
"id": "56",
"height": "470",
"url": "/services/asset/get/56/?token=",
"name": "JPG.jpg",
"DateModified": "2019-11-14T10:18:51Z"
},
{
"extension": "jpg",
"width": "0",
"caption": "second",
"id": "47",
"height": "0",
"url": "",
"name": "download--2.jpg",
"DateModified": "2019-10-10T10:55:34Z"
},
{
"extension": "jpg",
"width": "0",
"caption": "second",
"id": "47",
"height": "0",
"url": "",
"name": "download--2.jpg",
"DateModified": "2019-10-05T10:55:34Z"
}]
任何 jsfiddle 将不胜感激
let data ={
"jpg": [
{
"extension": "jpg",
"width": "450",
"caption": "first",
"id": "56",
"height": "470",
"url": "/services/asset/get/56/?token=",
"name": "JPG.jpg",
"DateModified": "2019-11-14T10:18:51Z"
},
{
"extension": "jpg",
"width": "0",
"caption": "second",
"id": "47",
"height": "0",
"url": "",
"name": "download--2.jpg",
"DateModified": "2019-10-10T10:55:34Z"
},
{
"extension": "jpg",
"width": "0",
"caption": "second",
"id": "47",
"height": "0",
"url": "",
"name": "download--2.jpg",
"DateModified": "2019-10-05T10:55:34Z"
}]
}
let array = data.jpg;
array.sort(function(a,b){
return new Date(a.DateModified) - new Date(b.DateModified);
});
console.log(array)
供你在reactjs中参考
https://jsfiddle.net/4mn2dept/
const images = {
"jpg": [
{
"extension": "jpg",
"width": "450",
"caption": "first",
"id": "56",
"height": "470",
"url": "/services/asset/get/56/?token=",
"name": "JPG.jpg",
"DateModified": "2019-11-14T10:18:51Z"
},
{
"extension": "jpg",
"width": "0",
"caption": "second",
"id": "47",
"height": "0",
"url": "",
"name": "download--2.jpg",
"DateModified": "2019-10-10T10:55:34Z"
},
{
"extension": "jpg",
"width": "0",
"caption": "second",
"id": "47",
"height": "0",
"url": "",
"name": "download--2.jpg",
"DateModified": "2019-10-05T10:55:34Z"
}]
}
const result = images.jpg.sort((a, b) => new Date(b.DateModified) - new Date(a.DateModified))
console.log(result)
我确实有一个 json,它以 DateModified 作为键,它的值为 UTC 格式。 我需要一个排序切换按钮,以便在切换时它应该根据这个日期排序(最新的在第一位) 谁能帮我弄明白。
这是 myJSON
{
"jpg": [
{
"extension": "jpg",
"width": "450",
"caption": "first",
"id": "56",
"height": "470",
"url": "/services/asset/get/56/?token=",
"name": "JPG.jpg",
"DateModified": "2019-11-14T10:18:51Z"
},
{
"extension": "jpg",
"width": "0",
"caption": "second",
"id": "47",
"height": "0",
"url": "",
"name": "download--2.jpg",
"DateModified": "2019-10-10T10:55:34Z"
},
{
"extension": "jpg",
"width": "0",
"caption": "second",
"id": "47",
"height": "0",
"url": "",
"name": "download--2.jpg",
"DateModified": "2019-10-05T10:55:34Z"
}]
任何 jsfiddle 将不胜感激
let data ={
"jpg": [
{
"extension": "jpg",
"width": "450",
"caption": "first",
"id": "56",
"height": "470",
"url": "/services/asset/get/56/?token=",
"name": "JPG.jpg",
"DateModified": "2019-11-14T10:18:51Z"
},
{
"extension": "jpg",
"width": "0",
"caption": "second",
"id": "47",
"height": "0",
"url": "",
"name": "download--2.jpg",
"DateModified": "2019-10-10T10:55:34Z"
},
{
"extension": "jpg",
"width": "0",
"caption": "second",
"id": "47",
"height": "0",
"url": "",
"name": "download--2.jpg",
"DateModified": "2019-10-05T10:55:34Z"
}]
}
let array = data.jpg;
array.sort(function(a,b){
return new Date(a.DateModified) - new Date(b.DateModified);
});
console.log(array)
供你在reactjs中参考
https://jsfiddle.net/4mn2dept/
const images = {
"jpg": [
{
"extension": "jpg",
"width": "450",
"caption": "first",
"id": "56",
"height": "470",
"url": "/services/asset/get/56/?token=",
"name": "JPG.jpg",
"DateModified": "2019-11-14T10:18:51Z"
},
{
"extension": "jpg",
"width": "0",
"caption": "second",
"id": "47",
"height": "0",
"url": "",
"name": "download--2.jpg",
"DateModified": "2019-10-10T10:55:34Z"
},
{
"extension": "jpg",
"width": "0",
"caption": "second",
"id": "47",
"height": "0",
"url": "",
"name": "download--2.jpg",
"DateModified": "2019-10-05T10:55:34Z"
}]
}
const result = images.jpg.sort((a, b) => new Date(b.DateModified) - new Date(a.DateModified))
console.log(result)