【问题标题】:Sort array of object in node js?对节点js中的对象数组进行排序?
【发布时间】:2020-04-16 14:48:36
【问题描述】:

我正在尝试根据 nodejs 中的 publishDate 对数组进行排序。脚本对具有publishDate 的数组正常工作,但对那些没有publishDate 的数组有问题。在核心 js 中,所有代码都可以正常工作,但现在在我们没有 publishDate 时可以在节点 js 中工作。请在下面查看我的代码:

代码:

const parseDate = 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',''));
}

const sortByPublishDate = array => {

    array.sort(function(a, b){
        if (a.publish  && b.publish) {
            return parseDate(a.publish.publishDate) - parseDate(b.publish.publishDate)

        }
        else if (a.hasOwnProperty("publish")) {
            return -1;
        } else if (b.hasOwnProperty("publish")) {
            return 1;            
        } else {
            return 0;

        }    
    } );
    return array;
}

module.exports ={
  sortByPublishDate: sortByPublishDate
}

数组:

[
    {

        "title": "Example1",
        "publish": {

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

    {

        "title": "Example2"
    },    

    {

        "title": "Example3",
        "publish": {           
            "publishDate": "2019-6-30T12:25:47.938Z",
        }
    },

    {

        "title": "Example4"
    },    

    {

        "title": "Example5",
        "publish": {           
            "publishDate": "2019-10-25T12:25:47.938Z",
        }
    }
    {

        "title": "Example6"
    }
] 

预期输出:Example3、Example1、Example5、Example2、Example4、Example6

【问题讨论】:

    标签: arrays node.js sorting


    【解决方案1】:

    尝试在排序前添加检查存在属性publish.publishDate

    if (a.publish && a.publish.publishDate  && b.publish && b.publish.publishDate) 
    {
            return parseDate(a.publish.publishDate) - parseDate(b.publish.publishDate);
    }
    

    一个例子:

    let arr = [
      {
    "title": "Example1",
    "publish": {
    
      "publishDate": "2019-8-30T12:25:47.938Z",
    }
      },
      {
    
    "title": "Example2"
      },
      {
    "title": "Example3",
    "publish": {
      "publishDate": "2019-6-30T12:25:47.938Z",
    }
      },
      {
    
    "title": "Example4"
      },
      {
    "title": "Example5",
    "publish": {
      "publishDate": "2019-10-25T12:25:47.938Z",
    }
      },
      {
    
    "title": "Example6"
      }
    ];
    
    const parseDate = 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', ''));
    }
    
    const sortByPublishDate = array => {
    
      array.sort(function (a, b) {
    if (a.publish && a.publish.publishDate && b.publish && b.publish.publishDate) {
      return parseDate(a.publish.publishDate) - parseDate(b.publish.publishDate)
    
    }
    else if (a.publish) {
      return -1;
    } else if (b.publish) {
      return 1;
    } else {
      return 0;
    
    }
      });
      return array;
    }
    
    console.log(sortByPublishDate(arr))

    【讨论】:

      猜你喜欢
      • 2020-06-29
      • 2015-10-19
      • 2018-09-05
      • 2021-01-19
      • 2012-05-02
      • 1970-01-01
      • 2014-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多