Sort array with lodash by value (integer)

后端 未结 2 694
暗喜
暗喜 2021-02-03 17:29

I\'m really struggling on that but I cannot find a solution.

I have an array and I want to sort it by value (all integers). I thought, well let\'s use lodash, there sure

2条回答
  •  误落风尘
    2021-02-03 18:07

    You can use the sortBy() function here. You don't have to specify a key, as it will fall back to identity().

    var myArray = [ 3, 4, 2, 9, 4, 2 ];
    
    _.sortBy(myArray);
    // → [ 2, 2, 3, 4, 4, 9 ]
    
    _(myArray).sortBy().take(3).value();
    // → [ 2, 2, 3 ]
    

提交回复
热议问题