Sort array of objects by single key with date value

后端 未结 19 1353
情话喂你
情话喂你 2020-11-22 10:56

I have an array of objects with several key value pairs, and I need to sort them based on \'updated_at\':

[
    {
        \"updated_at\" : \"2012-01-01T06:25         


        
19条回答
  •  感情败类
    2020-11-22 11:40

    You could use the Lodash utility library to solve this problem (it's quite an efficient library):

    const data = [{
        "updated_at": "2012-01-01T06:25:24Z",
        "foo": "bar"
      },
      {
        "updated_at": "2012-01-09T11:25:13Z",
        "foo": "bar"
      },
      {
        "updated_at": "2012-01-05T04:13:24Z",
        "foo": "bar"
      }
    ]
    
    const ordered = _.orderBy(
      data,
      function(item) {
        return item.updated_at;
      }
    );
    
    console.log(ordered)

    You can find documentation here: https://lodash.com/docs/4.17.15#orderBy

提交回复
热议问题