Printing array with console.log is giving different result from accessing directly

Asked

Viewed 89 times

0

Guys, when I print the 'vertical' array, I get the following:

(30) [0, 0, 0, 3.21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 104.11, 0, 0, 0, 0, _chartjs: Object, push: Function, pop: Function, shift: Function, splice: Function...]

Here’s the code:

<script type="text/javascript">
 var horizontal = [];
 var vertical = [];
 var backgroundColor = [];
 var borderColor = [];
 var ctx = document.getElementById("myChart").getContext('2d');

  for(i = 0 ; i < 30; i++) {
    vertical.splice(i, 0, 0);
  }

  $.get('Classes/buscarContas.php', function(dado) {
    dado = JSON.parse(dado);
    dado.forEach(function(obj) {
      var diaMesAno = obj['dtVencimento'].split('-');
      var dia = diaMesAno[2];
      //pega o valor atual
      var valor = parseFloat(obj['valor']);
      //corrige o zero a esquerda quando necessário
      if (dia < 10)
        dia = dia.replace('0', '');

      dia = parseInt(dia);

      vertical[dia] += valor;
    }); 
  });

console.log(vertical);

It’s funny that when I access position 3 of the array, it shows zero. I’m using this array to fill a graph and actually all values are at zero, but it seems that the array is not at all values at zero. What am I doing wrong?

Thank you

  • Where is the console.log?

  • Sorry, I just did. Thank you

  • And where you try to capture the third position?

  • I didn’t put it in the code, I just ran a test and deleted it later but I capture it using conspole.log(vertical[3]) in the same location as the console.log(vertical)

  • Also put an example json to simplify playback of the error. I see that in for is making vertical.splice(i, 0, 0);. The idea is to start with the values to 0 within that for ? If it is will be simpler to do directly vertical[i] = 0;

  • Yeah, the goal is to just zero. I will replace it with vertical[i] = 0. People, I thought there was an error in the array, but the same error is in the chartjs. He didn’t show anything on the chart before, so I thought it was weird. Now, when I zoom in or zoom out of the page or open the console, it shows the graphics. It’s some chartJs bug that has nothing to do with this topic. Thanks anyway for the help. I should finish this topic?

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.