Value substitution

Asked

Viewed 26 times

0

I am receiving an array in the variable "myObj" ([00, 12, 45, 34, 23, 34, 34, 34, 05, 42, 21, 11, 31]). And I needed to put this value in the series, that is, replace the array that is hardcoded there. Type: series = [myObj, ...]

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    var myObj = JSON.parse(this.responseText); // Valor que tenho
  }
};
xmlhttp.open("GET", "api/api_get_fogo.php", true);
xmlhttp.send();

var dataSales = {
  labels: ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'],
  series: [
    [00, 12, 45, 34, 23, 34, 34, 34, 05, 42, 21, 11, 31], // Onde quero substituir
    [00, 24, 36, 03, 34, 10, 15, 20, 48, 19, 49, 32, 02]
  ] 
};

  • It seems to me a simple question for someone who knows the least about Java, but I’m really not getting :v

  • 1

    Do not put code as image, instead put it as text formatting appropriately in the question editor. Regardless it is not at all clear what you are trying to trade and where.

  • 1

    Edited question, I hope it’s more noticeable...

  • 1

    These are asynchronous functions, the code inside the if is executed long after the dataSales have been executed.

1 answer

1


I’m not sure I understand the problem, but the instruction:

dataSales.series[0] = myObj;

doesn’t work?

If you want to add instead of replace you can also use push:

dataSales.series.push(myObj);
  • No, because "myObj" is a local variable...

  • Ah! I hadn’t really noticed the problem :/ So in the callback function of the request you can put the previously mentioned statements. xmlhttp.onreadystatechange = Function() { if (this.readyState == 4 && this.status == 200) { var myObj = JSON.parse(this.responseText); // Value I have dataSales.series[0] = myObj; } };

  • For, I had already tried it, however neither gives me error nor appears the intended result :v

  • 1

    Got it, thanks for the help!

Browser other questions tagged

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