Is there anything similar to python reversed range in lodash.
In python
list(reversed(range(0, 4)))
=> [3, 2, 1, 0]
list(reversed(range(3, 4)))
=> [3]
in lodash
console.log(_.range(3,4,-1))
[]
console.log(_.range(0, 4, -1));
[]
Is there anything similar to python reversed range in lodash.
In python
list(reversed(range(0, 4)))
=> [3, 2, 1, 0]
list(reversed(range(3, 4)))
=> [3]
in lodash
console.log(_.range(3,4,-1))
[]
console.log(_.range(0, 4, -1));
[]
Share
Improve this question
asked Apr 17, 2014 at 10:54
bsrbsr
58.8k88 gold badges218 silver badges321 bronze badges
4 Answers
Reset to default 7You have the start and stop values reversed.
console.log(_.range(3, -1, -1));
# [ 3, 2, 1, 0 ]
Alternatively you can use the chainable reverse
function, with the range
, like this
console.log(_.range(0, 4).reverse());
# [ 3, 2, 1, 0 ]
Note: Neither of them is similar to Python 3.x's range
function.
As of Lodash 4.0.0, there's a rangeRight method that populates values in descending order.
_.rangeRight(1, 5);
// => [4, 3, 2, 1]
Obviously, it should be
_.range(3,-1,-1)
_.range(3,2,-1)
Or with reverse
:
console.log(_.range(3,4).reverse())
console.log(_.range(0,4).reverse())
If you need a reversed list, the first parameter needs to be the higher number. This works:
console.log(_.range(4,0,-1); // [4, 3, 2, 1]