Skip to content Skip to sidebar Skip to footer

Arrays Javascript

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:

Solution 1:

For example:

arrChartValues = [];

for (var i=1;i<10;i++)
   arrChartValue.push(["Q" + i,i*10])

Solution 2:

var a= [];
for (i=0; i<20; i++){
  a[i]= ['q'+i, i];
}

Solution 3:

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

arrChartValues = newArray(['Q1', 20], ['Q2', 10], ['Q3', 30]);
arrChartValues.push(['Q4', 40]);

Solution 4:

var arrChartValues = [];

then in your loop:

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

which adds the value to your array

Post a Comment for "Arrays Javascript"