1

I want to make a loop that makes arrays automatically and assign the values to it. The problem is how to generate the array itself automatically.

for(var attGetter=1; attGetter <= num; attGetter++){
    var catesArray1 = new Array();

    for(var atttGetterArray=1; atttGetterArray <= series; attGetterArray++){
        idOfInput  = "cate"+chartGetter+"_series"+attGetterArray;
        catesArray1.push($("#"+idOfInput).val());
    }
}

I want the loop to generate the array itself automatically like catesArray1 catesArray2 catesArray3

and so on..

Develop Smith
  • 146
  • 1
  • 3
  • 13

6 Answers6

2

You need an object or an array to hold the multiple arrays you wish to create. Maybe something you are looking for is like the following?

var arrayHolder = new Array();

  for(var attGetter=1; attGetter <= num; attGetter++){
  var catesArray = new Array();

  for(var attGetterArray=1; atttGetterArray <= series; attGetterArray++){
    idOfInput  = "cate"+chartGetter+"_series"+attGetterArray;
    catesArray.push($("#"+idOfInput).val());
  }
  arrayHolder.push(catesArray);
}
Kyle Weller
  • 2,533
  • 9
  • 35
  • 45
  • so the only choice is to make two-dimensional array. I can't to find another way to auto-generate arrays – Develop Smith Feb 26 '13 at 03:52
  • You cannot create new arrays referenced by names with the value of your loop counter if that is what you are trying to do. – Kyle Weller Feb 26 '13 at 03:54
  • Yes you can see this http://jsfiddle.net/farrukhsubhani/KudTe/2/ you can create them using eval – Farrukh Subhani Feb 26 '13 at 04:17
  • @Farrukh Subhani, I also just read this [article](http://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea) about why you have to be careful doing that... – Kyle Weller Feb 26 '13 at 04:24
1

If you want the arrays to be in global namespace, You can try

window['catesArray' + attGetter] = [];
...
window['catesArray' + attGetter].push(...)

Else you can create a hash object and use it to hold the reference

var obj = {};
.....
obj['catesArray' + attGetter] = [];
.....
obj['catesArray' + attGetter].push(...)
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • Yes, again there are multiple answers which deals with solving the problem using two dimensional arrays. It is another way to solve the problem by using dynamic variables – Arun P Johny Feb 26 '13 at 03:57
1

In that case you will have to create one new array that holds all the cacatesArrays from first for loop

var catesArrayContainer = new Array(); //<<<---------------------
for(var attGetter=1; attGetter <= num; attGetter++){
    var catesArray = new Array();

    for(var atttGetterArray=1; atttGetterArray <= series; attGetterArray++){
        idOfInput  = "cate"+chartGetter+"_series"+attGetterArray;
        catesArray.push($("#"+idOfInput).val());
    }
    catesArrayContainer.push(catesArray); //<<<--------------------
}

EDIT :

This happens because the scope of variable catesArray1 was limited. When the loop enters next iteration the catesArray1 gets reinitialized, thus losing all the previously stored values... Now in the code I have posted, we are storing every instance of catesArray1 in another array, and your values persist out side of the for loop

AdityaParab
  • 7,024
  • 3
  • 27
  • 40
  • so the only choice is to make two-dimensional array. I can't to find another way to auto-generate arrays – Develop Smith Feb 26 '13 at 03:52
  • As someone reminded me, the scope of catesArray1 is not limited to the for loop, but you are absolutely right about it being reinitialized and losing it's values – Kyle Weller Feb 26 '13 at 03:52
  • That depends, What do you want to achieve exactly? From the question you have posted, using `2D array` sounds perfect solution – AdityaParab Feb 26 '13 at 03:55
  • @KyleWeller : Yes, It has `function` scope... But since OP hasn't specified the exact location of the code, I answered considering the `js` file only contains the code in the question. – AdityaParab Feb 26 '13 at 03:57
  • also : understand this... in `this` particular case, the scope of `catesArray` `IS` limited to for lopp.. Why? because, it has been declared with `var`... If `var` wasn't there, then it'd have been accessible from outside of that for loop.. – AdityaParab Feb 26 '13 at 03:58
  • variables declared with `var` have block scope... if the variable is not declared using `var` then it is treated as `global` – AdityaParab Feb 26 '13 at 03:59
1

You can do something like this for 4 arrays of 5 elements each

yourarray=[];
for (i = 0; i <4; i++) {
     temparray=[];
    for (j = 0; j < 5; j++) {
        temparray.push($('#'+whateverID+'_'+i+'_'+j)) //your values here
    }
yourarray.push(temparray);
}

Check it on this JSFiddle. open chrome console to see array

If you want to create array within loop from index

You can use eval to evaluate javascript from strings but i wont use that unless there is no other way. you can see both above and eval method in this Fiddle. Open Chrome console to see array values Just a comparison of using eval and 2D array Open console in chrome while you run this jsFiddle and you will see the difference in eval and 2darray in context of this question.

Farrukh Subhani
  • 2,018
  • 1
  • 17
  • 25
0

You should assign them to an object. In this case, make an object variable before the first for-loop to hold all arrays:

var allArrays = {};

for(var attGetter=1; attGetter <= num; attGetter++){
    var currentArray = allArrays['catesArray' + attGetter] = new Array();

    for(var atttGetterArray=1; atttGetterArray <= series; attGetterArray++){
        idOfInput  = "cate"+chartGetter+"_series"+attGetterArray;
        currentArray.push($("#"+idOfInput).val());
    }
}
Laith
  • 6,071
  • 1
  • 33
  • 60
0

Instead of attempting to create & allocate dynamically named variables, I would think of this more of an array of array's if you will. In other words, create an array that holds all of the arrays you want:

var collections = []; // Literal notation for creating an array in JS

From here, it's a matter of making each value you create within this array its own array:

var n = 10; // Total amount of arrays you want
for (var i = 0; i < n; i++) {
    var values = [];
    // Have another loop that fills in the values array as expected
    collections.push(values); // Each element at collections[i] is its own array.
}

If you truly need named elements, you could potentially do something very similar with just an object {} instead, and refer to each element by a name you create.

var collections = {}; // Literal notation for an object in JS
var n = 10; // Total amount of arrays you want
for (var i = 0; i < n; i++) {
    var values = []; // Literal notation for an array in JS
    // Fill in the values array as desired

    var name = 'arr' + i; // How you'll refer to it in the object
    collections[name] = values;
}

I suggest the former though, since it does not sound like you need to have explicit names on your arrays, but just want multiple layers of arrays.

pseudoramble
  • 2,541
  • 22
  • 28