javascript - 如果JS中存在值,如何根据日期按降序对数组进行排序?
问题描述
我正在尝试根据降序对数据进行排序,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
and的位置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 - 设置高度时缺少制表符滚动条
- django - 模型关系 Django
- r - 使用 dplyr/tidyverse 同时对多个变量进行多对 t 检验
- ms-access - 如何将 2 个文本框加到表单上的 3 个文本框中
- regex - NiFi ReplaceText:去除特定标签之间的所有 xml 标签
- machine-learning - 使用机器学习对图像文档进行分类
- php - 使用 queryBuidler 运行 get() 时,stdClass 类的对象无法转换为字符串
- docker - 在 docker 容器中运行时找不到 Sendmail 主机
- python - 用三个条件删除重复项,Pandas
- r - 具有多个参数的 R 双积分