简体   繁体   English

使用lodash反转范围

[英]reversed range using lodash

Is there anything similar to python reversed range in lodash . lodash中是否有类似于python反转范围的东西

In python 在python中

   list(reversed(range(0, 4)))
=> [3, 2, 1, 0]
   list(reversed(range(3, 4)))
=> [3]

in lodash 在lodash

 console.log(_.range(3,4,-1))
[]
   console.log(_.range(0, 4, -1));
[]

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 或者你也可以使用可链接的reverse功能, range就像这样

console.log(_.range(0, 4).reverse());
# [ 3, 2, 1, 0 ]

Note: Neither of them is similar to Python 3.x's range function. 注意:它们都不类似于Python 3.x的range函数。

As of Lodash 4.0.0, there's a rangeRight method that populates values in descending order. 从Lodash 4.0.0开始,有一个rangeRight方法按降序填充值。

_.rangeRight(1, 5);
// => [4, 3, 2, 1]

Obviously, it should be 显然,它应该是

_.range(3,-1,-1)
_.range(3,2,-1) 

Or with reverse : 或者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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM