gpt4 book ai didi

javascript - 如果JS中存在值,如何根据日期按降序对数组进行排序?

转载 作者:行者123 更新时间:2023-12-04 02:04:32 26 4
gpt4 key购买 nike

我正在尝试根据 publishDate 的降序对数据进行排序但它不起作用。在某些数组中,publishDate来了,在某些阵列中,它没有来。

[
{
"id": "brexit-delay",
"title": "Brexit Delay",
"publish": {

"publishDate": "2019-8-30T12:25:47.938Z",
}
},

{
"id": "brexit-delay",
"title": "Example 3"
},

{
"id": "brexit-delay",
"title": "Example 2",
"publish": {
"publishDate": "2019-6-30T12:25:47.938Z",
}
},

{
"id": "brexit-delay",
"title": "Example 5"
},

{
"id": "brexit-delay",
"title": "Example 5",
"publish": {
"publishDate": "2019-10-25T12:25:47.938Z",
}
}
]

js代码:
data.sort(function(a, b){
if("publish" in a && "publish" in b){
return new Date(a.publish.publishDate) - new Date(b.publish.publishDate)
}
} );

最佳答案

检查该属性是否存在并基于此退货单:

const myParseDate = date_string => {
let [y,M,d,h,m,s] = date_string.split(/[- :T]/);
return new Date(y,parseInt(M)-1,d,h,parseInt(m),s.replace('Z',''));
}

arr.sort(function(a, b){
if (a.publish && b.publish) {
return myParseDate(b.publish.publishDate) - myParseDate(a.publish.publishDate)
}
else if (a.hasOwnProperty("publish"))
return -1;
else if (b.hasOwnProperty("publish"))
return 1;
else
return 0;
} );

一个例子:

const arr = [
{
"id": "brexit-delay",
"title": "Brexit Delay",
"publish": {
"publishDate": "2019-8-30T12:25:47.938Z",
}
},
{
"id": "brexit-delay",
"title": "Example 3"
},
{
"id": "brexit-delay",
"title": "Example 2",
"publish": {
"publishDate": "2019-6-30T12:25:47.938Z",
}
},
{
"id": "brexit-delay",
"title": "Example 5"
},
{
"id": "brexit-delay",
"title": "Example 5",
"publish": {
"publishDate": "2019-10-25T12:25:47.938Z",
}
}
]

const myParseDate = date_string => {
let [y,M,d,h,m,s] = date_string.split(/[- :T]/);
return new Date(y,parseInt(M)-1,d,h,parseInt(m),s.replace('Z',''));
}

arr.sort(function(a, b){
if (a.publish && b.publish)
return myParseDate(b.publish.publishDate) - myParseDate(a.publish.publishDate)
else if (a.hasOwnProperty("publish"))
return -1;
else if (b.hasOwnProperty("publish"))
return 1;
else
return 0;
} );

console.log(arr);


更新:

如果要升序,则只需更改 a 的位置和 b :
arr.sort(function(a, b){
if (a.publish && b.publish)
return myParseDate(a.publish.publishDate) - myParseDate(b.publish.publishDate)
else if (a.hasOwnProperty("publish"))
return -1;
else if (b.hasOwnProperty("publish"))
return 1;
else
return 0;
} );

一个例子:

const arr = [
{
"id": "brexit-delay",
"title": "Brexit Delay",
"publish": {
"publishDate": "2019-8-30T12:25:47.938Z",
}
},
{
"id": "brexit-delay",
"title": "Example 3"
},
{
"id": "brexit-delay",
"title": "Example 2",
"publish": {
"publishDate": "2019-6-30T12:25:47.938Z",
}
},
{
"id": "brexit-delay",
"title": "Example 5"
},
{
"id": "brexit-delay",
"title": "Example 5",
"publish": {
"publishDate": "2019-10-25T12:25:47.938Z",
}
}
]

const myParseDate = date_string => {
let [y,M,d,h,m,s] = date_string.split(/[- :T]/);
return new Date(y,parseInt(M)-1,d,h,parseInt(m),s.replace('Z',''));
}

arr.sort(function(a, b){
if (a.publish && b.publish)
return myParseDate(a.publish.publishDate) - myParseDate(b.publish.publishDate)
else if (a.hasOwnProperty("publish"))
return -1;
else if (b.hasOwnProperty("publish"))
return 1;
else
return 0;
} );

console.log(arr);

关于javascript - 如果JS中存在值,如何根据日期按降序对数组进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59421376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com