Javascript/jQuery 中的范围?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11809114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Range in Javascript/jQuery?
提问by Memochipan
Possible Duplicate:
Does JavaScript have a range() equivalent?
Is there a way to declare a range in Javascript/jQuery like we do in Python?
有没有办法像在 Python 中那样在 Javascript/jQuery 中声明一个范围?
Something like this:
像这样的东西:
x = range(1,10)
x = 范围(1,10)
x = [1,2,3,4,5,6,7,8,9]
x = [1,2,3,4,5,6,7,8,9]
Thanks.
谢谢。
采纳答案by Jaro
By using some third party libraries like Underscore.js you can achieve the same behaviour as in Python http://underscorejs.org/#range
通过使用 Underscore.js 等第三方库,您可以获得与 Python http://underscorejs.org/#range相同的行为
回答by Daniel A. White
You simply can create an array, loop over the values using a for
loop and push
ing the values. There isn't anything built into the language.
您只需创建一个数组,使用循环遍历这些值for
并push
ing 值。该语言没有内置任何内容。
回答by Alex W
Put this function in your Javascript code, and you should be able to call range()
like you do in Python (but it only works for numbers):
将此函数放在您的 Javascript 代码中,您应该能够range()
像在 Python 中一样调用(但它仅适用于数字):
function range(start, end)
{
var array = new Array();
for(var i = start; i < end; i++)
{
array.push(i);
}
return array;
}