Search code examples
javascriptarraysapidatatablegoogle-visualization

Error when combining two arrays in a datatable in Google Chart API


The same Value (PIB) is repeating in every row when it should assign one Value to one Date. What I am doing wrong?

enter image description here

I have this 2 arrays from a JSON url:

    var date = [array1];
    var number = [array2];

I create the Table and add two columns:

    var figure = new google.visualization.DataTable();
    figure.addColumn('date', 'Date');
    figure.addColumn('number', 'PIB');

And I am sure here is the problem, I create loops to parse both arrays and try to combine them with figure.addRow:

  for (j in num){
      var myVal = parseFloat(number[j]); 
  };
  
  for (i in date){
      var dated = new Date(date[i]);
      figure.addRow([dated, myVal]);
  };  
  

Full code in my fiddle: (https://jsfiddle.net/Enrique_94/khrg0ume/3/ )

Thank you!


Solution

  • It works with just one loop and same index

        for (i in num){
            var myVal = parseFloat(num[i]); 
            var dated = new Date(date[i]);
            figure.addRow([dated, myVal]);
            };