JQuery: how to create an array with a loop

I am trying to create an array in jQuery that fills through a loop.

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

With the code above, I get an integer value (e.g. 5). I would like to know 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, at the end of my array, for example, 5 would look like this: [1,2,3,4,5].

+3
source share
5 answers

Description

This is more about javascript, not jquery. Check out my sample and jsFiddle Demo

Example

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

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

alert(arr1.length)

Additional Information

+5
source

firstly, it val()will return a string, so we break it into an integer

var count = parseInt(jQuery('#count_images').val(),10);

Then you can simply use the loop to create the array:

var arr = [];
for(var i=0;i<count;i++){
   arr.push(i);
}

[0,1,2,3,4], , 1, 1 i

var arr = [];
for(var i=0;i<count;i++){
   arr.push(i+1);
}
0

- :

var my_array = [];
var count = 5;      // let assume 5

for(var i=0; i < count; i++) {
    my_array.push(i);
}
0

jQuery . :

arr1 = []; 
for (var i = 0; i < count; i++) arr1[i] = i + 1;
// arr1 = [1, 2, 3, 4, 5]
0
var days = $.map(new Array(31), function(item, index){return index+1;});
0

All Articles