Question

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));
[]
Was it helpful?

Solution

You 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.

OTHER TIPS

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]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top