use Lodash to sort array of object by value

后端 未结 2 456
伪装坚强ぢ
伪装坚强ぢ 2020-12-04 09:31

I am trying to sort an array by \'name\' value (using Lodash). I used the Lodash docs to create the solution below however .orderBy doesn\'t seem to be having any affect at

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-04 09:57

    You can use lodash sortBy (https://lodash.com/docs/4.17.4#sortBy).

    Your code could be like:

    const myArray = [  
       {  
          "id":25,
          "name":"Anakin Skywalker",
          "createdAt":"2017-04-12T12:48:55.000Z",
          "updatedAt":"2017-04-12T12:48:55.000Z"
       },
       {  
          "id":1,
          "name":"Luke Skywalker",
          "createdAt":"2017-04-12T11:25:03.000Z",
          "updatedAt":"2017-04-12T11:25:03.000Z"
       }
    ]
    
    const myOrderedArray = _.sortBy(myArray, o => o.name)
    

提交回复
热议问题