0
for(var j =0; j<data.length; j++){
var valorEmBranco = 0;
for(var i =0; i<data[j].length; i++){
var porcentagem = 0;
var valorPorColuna = data[j][i].valor;
//alert(valorPorColuna);
if(data[j][i].nome ===""){
valorEmBranco += data[j][i].valor;
} else {
porcentagem = (valorPorColuna / (totalDados-valorEmBranco)) * 100;
}
data[j][i]['porcentagem'] = porcentagem;
}
}
https://jsfiddle.net/zm45Lywo/4/
I’m having trouble calculating percentages. I need to "sift" the blank names and remove them from the total value to make the percentage calculation. Let’s say I have 4253 elements in total, I need to subtract from this value every time the name equals empty ("); that is, (4253-numbersEmBranco), which in this case is 12 (by jsfiddle)
my multidimensional array:
var data = [[{
"nome": "SIM",
"valor": 364
},{
"nome": "NÃO",
"valor": 3877
},{
"nome": "",
"valor": 12
}]];
porcentagem = (valorPorColuna / (totalDados-valorEmBranco)) * 100;
The calculation is not working. by the rule of 3, the correct for names with "NO" = 91,41%
and is giving 91,16%
.
I didn’t understand, if the value of the name field is empty it is not to consider even if in this index has a value ?
– Gabriel Rodrigues
should be disregarded the empty (or blank) values (names). That is, in total I have 4253 elements, I must disregard the blank names. 4253-12 = 4241. Oh, I make the rule of 3 for the percentage. (it didn’t work)
– carlos