I'm trying to sum the specific value from json array:
([{"DESPRO":"PLANILLA DE EMPLEADOS","ANIO":"2013","MES":"06","NROPER":"201306","PLAGRPCON":"3","GRUPO_CONCEPTO":"APORTES","CODCON":"3010 ","CONCEPTO":"ESSALUD ","VALOR":"106,1"},
{"DESPRO":"PLANILLA DE EMPLEADOS","ANIO":"2013","MES":"06","NROPER":"201306","PLAGRPCON":"2","GRUPO_CONCEPTO":"DESCUENTOS","CODCON":"2030 ","CONCEPTO":"ADELANTO DE QUINCENA ","VALOR":"350"},
{"DESPRO":"PLANILLA DE EMPLEADOS","ANIO":"2013","MES":"06","NROPER":"201306","PLAGRPCON":"2","GRUPO_CONCEPTO":"DESCUENTOS","CODCON":"2604 ","CONCEPTO":"ADE. MOVILIDAD 1ER","VALOR":"48"},
{"DESPRO":"PLANILLA DE EMPLEADOS","ANIO":"2013","MES":"06","NROPER":"201306","PLAGRPCON":"2","GRUPO_CONCEPTO":"DESCUENTOS","CODCON":"2605 ","CONCEPTO":"ADE. MOVILIDAD 2DO","VALOR":"56"},
EMPLEADOS","ANIO":"2013","MES":"06","NROPER":"201306","PLAGRPCON":"2","GRUPO_CONCEPTO":"DESCUENTOS","CODCON":"2090 ","CONCEPTO":"AFP-PRIMA DE SEGURO","VALOR":"16,27"}, .... etc etc
In this case sum the VALORs from the array and put in any variable on javascript.
I got this code:
// some JS code
for (var i in datos){
var HABERES = parseInt(datos[i].VALOR, 10);
HABERES += HABERES;
alert(HABERES);
}
When the alert goes, the result is not the correct..
Is there any problem(s) with that code?
Thank for answers..
It's hard to tell what you actually want, but I think it's this. Start with
HABERES
at 0 and add the values in the loop.http://jsfiddle.net/qGYTH/1/
Replace
,
with.
then
parseFloat
HABERES.replace('.',',');
if you want comma on the result.javascript uses the . as decimal separator.
parseFloat returns also decimal values.
http://jsfiddle.net/qGYTH/3/ Explosion Pills fiddle with my code.
Should be something like;