Replace in the javascript array

Asked

Viewed 801 times

1

Good afternoon, friends I’m having a problem here in javascript. I have an array, but with each Dice it is adding a double quote, and I want to remove them from there. My code is like this:

    dados = new Array();
       $.get(url, function(response){
       for(i in response.content) {
           horario = parseInt(response.content[i].Horario);
           tempo = parseInt(response.content[i].Tempo);
           dados[i]= "["+ horario +", "+ tempo  + "]";
           dados[i] = dados[i].replace(' " ', ' ');
       }

       console.log(dados);

   });

And my return is being so

["[0, 54]", "[0, 65]", "[10, 60]"]

And it had to be so:

[[0, 54], [0, 65], [10, 60]]

Does anyone know if why the replace is not working?

EDIT

I forgot to mention the important part, I will use this array for a Chart and the data model he reads is like this:

data: [ [0, 54], [5, 65], [10, 60] ]

so I already concatenei there in it opens "keys" in each of the indices

  • Luan the way you are doing this creating a monodimensional array of String, and what you want is a two-dimensional array of Int, for that you will have to assign in dados[i] the array with the time,

3 answers

1

Good let’s convert the comment to practice:

Luan the way you are doing this creating a monodimensional array of String, and what you want is a two-dimensional array of Int, for that you will have to assign in data[i] the array with the time,time

dados = new Array();
    $.get(url, function(response){
    for(i in response.content) {
            horario = parseInt(response.content[i].Horario);
            tempo = parseInt(response.content[i].Tempo);
            dados[i] = new Array();
            dados[i].push(horario);
            dados[i].push(tempo);
    }
    console.log(dados);
});
  • Hello William, thanks for the help. So, I have to play this array on a Chart.js that has as default date the way I sent it there. It would look like this: date: [ [1,20], [2,60] . I edited my question to explain better

  • the way you put it, how do I play on the Chart date in this format that it needs?

  • @Luangabriel would have to specify which library you are using, but I believe only chart['data'] = dados;

  • I’m using Chart.js

1

All the answers solved your problem, but they didn’t answer what you asked, so here’s the answer to your question, just for future cases (what they answered is correct)

Your replace did not work due to syntax error.

the correct form would be

data[i] = data[i]. replace(/"/, '');

The value to be replaced has to be surrounded by bars, and if you want to remove something you need to put an empty string after the comma, not a string with a space inside (otherwise you will have a space before and one after each key).

0

The point is that when you replace '"' with ' you still have a string array. Where each record contains a set of [number, number]. And then the browser console is playing a trick on you. The way the console presents a string array will always be ["valor1", "valor2"] and so on...

This gives the impression that replace is not working when it actually is. But as I said it is still a string array. If you put a console showing only replace you will see that it is right.

To summarize use Eval to transform the string array into number array see:

var dados = [];
for(i in [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) {
	dados[i]= "["+ i +", "+ 5  + "]";
}

console.log(dados);

for(i in [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) {
	dados[i] = eval(dados[i]);
}

console.log(dados);

Browser other questions tagged

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