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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:25:10  来源:igfitidea点击:

Range in Javascript/jQuery?

javascriptjqueryarrays

提问by Memochipan

Possible Duplicate:
Does JavaScript have a range() equivalent?

可能的重复:
JavaScript 是否有一个 range() 等价物?

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 forloop and pushing the values. There isn't anything built into the language.

您只需创建一个数组,使用循环遍历这些值forpushing 值。该语言没有内置任何内容。

回答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;
}