use Lodash to sort array of object by value

后端 未结 2 449
伪装坚强ぢ
伪装坚强ぢ 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:54

    This method orderBy does not change the input array, you have to assign the result to your array :

    var chars = this.state.characters;
    
    chars = _.orderBy(chars, ['name'],['asc']); // Use Lodash to sort array by 'name'
    
     this.setState({characters: chars})
    

提交回复
热议问题