jQuery: how to make an array with a loop

17,710

Description

This is more about javascript and not jquery. Check out my sample and this jsFiddle Demonstration

Sample

var arrLength = 5;
var arr1 = [];
var i = 0;

for (i = 0; i != arrLength; i++){
  arr1.push(i)
}

alert(arr1.length)

More Information

Share:
17,710
DCMetaZero
Author by

DCMetaZero

Updated on June 29, 2022

Comments

  • DCMetaZero
    DCMetaZero almost 2 years

    I am trying to create an array in jQuery, which is filled through a loop.

    count = jQuery('#count_images').val();
    

    With the above code I get an integer value (such as 5, for example). What I would like to know, is how I can do something like this in jQuery:

    int arrLength = count;
    string[] arr1 = new string[arrLength];
    int i = 0;
    for (i = 0; i < arr1.Length; i++){
        arr1[i] = i;
    }
    

    So in the end my array for example 5 would look like this: [1,2,3,4,5].

    • ant
      ant about 12 years
      did you try looking at api.jquery.com/jQuery.each
    • DCMetaZero
      DCMetaZero about 12 years
      I did, but that was not what I was looking for though. Anyways, I used dknaack's solution, and that works fine for me. Thanks anyways.