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;
}
}
I'm having trouble calculating percentage. I need to "sift" the blank names and remove them from the total amount to do the percentage calculation. Let's say I have 4253 elements in total, I need to subtract from that value every time the name is equal to empty (""); That is, (4253-numberEmBranco), 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 the names with "NO" = 91,41%
and is giving 91,16%
.