0

I'm using google charts to generate charts, where I want to use array data. It works fine when I use the array variable with index. But when I am looping through the array, only the last index value of the array is shown.

   for(var i=0;i<myTableArray.length;i++)
   {
      var data = google.visualization.arrayToDataTable([
      ['Year', 'Sales', 'Expenses'],
      ['2013',  1000,      400],
      ['2014',  1170,      460],
      ['2015',  660,       1120],
      ['2016',  1030,      540],
      ['2019',1233,122],
      myTableArray[0],  //works 
      myTableArray[1], //works
      myTableArray[i]   // value of myTableArray[1] is shown
      ]);
      alert(myTableArray[i]);
   }
8
  • 2
    Possible duplicate of JavaScript closure inside loops – simple practical example Commented Apr 20, 2017 at 5:43
  • What is the length of your myTableArray array? Commented Apr 20, 2017 at 5:43
  • length of the array is 2 Commented Apr 20, 2017 at 5:53
  • What’s in myTableArray? Commented Apr 20, 2017 at 5:58
  • 1
    myTableArray=["xyxy","1212","21212"]; Commented Apr 20, 2017 at 6:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.