-1

I seem to be going around in circles on something I know is really easy. But I must be having a bad day.

I want to populate a 2d array dynamically.

If I do it statically like this:

arrChartValues = new Array(['Q1', 20], ['Q2', 10], ['Q3', 30]);

But I want it to be in a loop inserting lots of pairs of values.

hakre
  • 193,403
  • 52
  • 435
  • 836
Oliver
  • 809
  • 1
  • 11
  • 18
  • Do you want to know how to loop over multiple axes, set elements of the 2D array, set elements of the 1D arrays or something else? – outis Jan 18 '10 at 13:28

4 Answers4

4

For example:

arrChartValues = [];

for (var i=1;i<10;i++)
   arrChartValue.push(["Q" + i,i*10])
Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
0

If you want to add new items to an existing array, use the push method like this:

arrChartValues = new Array(['Q1', 20], ['Q2', 10], ['Q3', 30]);
arrChartValues.push(['Q4', 40]);
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • I don't know how many data pairs there will be so I need to build the array dynamically in a loop. – Oliver Jan 18 '10 at 13:28
0
var a= [];
for (i=0; i<20; i++){
  a[i]= ['q'+i, i];
}
naivists
  • 32,681
  • 5
  • 61
  • 85
0
var arrChartValues = [];

then in your loop:

arrChartValues.push(['Q1', 20]);

which adds the value to your array

Josh
  • 6,256
  • 2
  • 37
  • 56