Reputation: 1556
Is there any way to make _.orderBy
alter the provided array? Using the example below it only seems to return a sorted result, leaving the provided array intact.
var arr = [{x: 1},{x: 2}];
console.log(_.orderBy(arr, 'x', 'desc')[0].x, arr[0].x);
https://jsfiddle.net/w5hoeurs/
Upvotes: 0
Views: 3496
Reputation: 642
As per Lodash documentation on orderBy:
Returns (Array): Returns the new sorted array.
So, your code would be:
var arr = [{x: 1},{x: 2}];
arr = _.orderBy(arr,['x'],['desc']);
console.log(arr);
Upvotes: 3