How to remove double quotes from a JS array

Asked

Viewed 106 times

0

Lately I’ve been trying to use a Highcharts chart but I’ve been facing a problem, where I pass the value for graph generation does not accept "", and my array I capture from php is mounted with "" for example "1","2","3" and there works my code I need it to be 1,2,3 in the array follows my code next

      xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      rcbCom = this.responseText;              // rcbCom = recebe o conteudo da variaves do php e começa a fazer a quebra da informação, rcb = recebe
      var spCom = rcbCom.split("|");           // spCom = dividi as colunas separando onde estao as | ,    sp=Separacao
      dataon[0]=  spCom[0].split(",");         // numero do simcon
      sinal[0]= spCom[1].split(",");
      on[0]= spCom[2].split(",");


      Highcharts.chart('graf', {

  title: {
    text: 'QUEDAS DE CONEXÃO'
  },

  xAxis: {
    tickInterval: 1,
    type: 'logarithmic',
    accessibility: {
      rangeDescription: 'Range: 1 to 10'
    }
  },

  yAxis: {
    type: 'logarithmic',
    minorTickInterval: 0.1,
    accessibility: {
      rangeDescription: 'Range: 0.1 to 1000'
    }
  },

  tooltip: {
    headerFormat: '<b>{series.name}</b><br />',
    pointFormat: 'x = {point.x}, y = {point.y}'
  },
  series: [{
    data: [1,2,3,4], //aqui preciso passar minha variavel on[0] porem com aspas ela não funciona
    pointStart: 1
  }]
});



    }
  }
  xmlhttp.open("GET","../GrafCom/php/graf.php?cod="+cod+"&srv="+srv+"&data1="+date1+"&data2="+date2+"&con="+nCon, true);       //para capturar o valor da variavel cd e mandar para o php
  xmlhttp.send();
  • 1

    where the PHP code?

1 answer

1


Hello, my suggestion for you is to insert a loop to iterate over the array right after receiving it, converting it into a numerical array.

Try the following snippet by replacing 'main' with the name associated with your array:

var main = ["1", "2", "3", "4", "5"];

var cMain = main.map(Number)

Or, you can use a for loop if you don’t want to create another variable:

var main = ["1", "2", "3", "4", "5"];

for (let i = 0; i < main.length; i++) {main[i] = Number(main[i])}

If you want better performance, declare 'Let i' out of the loop by using '{}' to control Scope.

Ps: I don’t know much about the context of your code, but I hope this helps.

Browser other questions tagged

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