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– Costamilam
Now returns an array with two arrays. (2) [Array(2), Array(2)]
– Peter Almeida
If that’s not the point, what is?
– Costamilam
Return arrays separated by comma: ['carlos Alberto', 1],['Bahia folia', 2],
– Peter Almeida
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 (")
– Costamilam