Javascript array problem

Asked

Viewed 120 times

1

I am facing a problem with javascript array. I am using Google Charts and to popular the chart, an array is required containing the name and participation.

function contentChart(){
var id = document.querySelectorAll('.id')
var nome = document.querySelectorAll('.nome')
var sobrenome = document.querySelectorAll('.sobrenome')
var participacao = document.querySelectorAll('.participacao')

var chartContent = [];
for (i = 0; i < id.length ; i++) {
    chartContent += `['${nome[i].innerText} ${sobrenome[i].innerText}', ${participacao[i].innerText}],`;
    /*var result = chartContent.push([`${nome[i].innerText} ${sobrenome[i].innerText}`, participacao[i].innerText],)*/
}
return chartContent }

The above function creates a variable and assigns the obtained values, almost working. However, by returning a string gives error.

Retorno : "['carlos alberto', 1],['bahia folia', 2]," 

If I came without the quotes it would work.

ex: ['carlos alberto', 1],['bahia folia', 2],

Error: jsapi_compiled_default_module.js:178 Uncaught (in Promise) Error: Invalid Row #1 At object.gvjs_ql [as arrayToDataTable] (jsapi_compiled_default_module.js:178) at drawChart (main.js:9)

  • In the commented line is the right way, just remove the last "," from the end. Only you don’t have to create a variable, just do the push and gave

  • Now returns an array with two arrays. (2) [Array(2), Array(2)]

  • If that’s not the point, what is?

  • Return arrays separated by comma: ['carlos Alberto', 1],['Bahia folia', 2],

  • So what do you want a string with a JSON of a two-dimensional array? If that’s the way you did it is almost correct, just exchange the single quotes (') for doubles (")

1 answer

1

Look, I’m not sure what you want to do, but there are two possibilities that I’ve imagined. Preserve the content of chartContent and use the function val. But it should preserve the variable type as Array and not turning her into a String.

Altered code:

for (i = 0; i < id.length ; i++) {
    chartContent.push(eval(`['${nome[i].innerText} ${sobrenome[i].innerText}', ${participacao[i].innerText}],`));
}

But I recommend this way:

function contentChart() {
       const id = document.querySelectorAll('.id');
       const nome = document.querySelectorAll('.nome');
       const sobrenome = document.querySelectorAll('.sobrenome');
       const participacao = document.querySelectorAll('.participacao');

       return id.map((valor, i) => [`${nome[i].innerText} ${sobrenome[i].innerText}`, participacao[i].innerText]);
}

Considerations:

  1. Placed const because the variables are immutable;
  2. Instead of the for, inserted the map. He’s more elegant and faster than the foreach;
  3. From what I understand the participation is always number, then I removed the quotes.

Good studies!

  • 2

    It may be more elegant, but it’s impossible that the map is faster than the for, because, in general, a map is implemented from a for.

  • Indeed, I edited my answer. Valeus.

Browser other questions tagged

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