Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Range in Javascript/jQuery? [duplicate]

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

Is there a way to declare a range in Javascript/jQuery like we do in Python?

Something like this:

x = range(1,10)

x = [1,2,3,4,5,6,7,8,9]

Thanks.

like image 684
Memochipan Avatar asked Aug 04 '12 13:08

Memochipan


3 Answers

By using some third party libraries like Underscore.js you can achieve the same behaviour as in Python http://underscorejs.org/#range

like image 141
Jaro Avatar answered Nov 10 '22 19:11

Jaro


You simply can create an array, loop over the values using a for loop and pushing the values. There isn't anything built into the language.

like image 3
Daniel A. White Avatar answered Nov 10 '22 17:11

Daniel A. White


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):

function range(start, end)
{
    var array = new Array();
    for(var i = start; i < end; i++)
    {
        array.push(i);
    }
    return array;
}
like image 2
Alex W Avatar answered Nov 10 '22 19:11

Alex W