Sort array of objects by single key with date value

后端 未结 19 1370
情话喂你
情话喂你 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:59

    Use underscore js or lodash,

    var arrObj = [
        {
            "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"
        }
    ];
    
    arrObj = _.sortBy(arrObj,"updated_at");
    

    _.sortBy() returns a new array

    refer http://underscorejs.org/#sortBy and lodash docs https://lodash.com/docs#sortBy

    0 讨论(0)
提交回复
热议问题