Add values obtained in javascript

2

Good morning.

How can I make the sum of values obtained from JSON and interpreted with JS. In the following code I get my data brought with json and I go through it with the for to show them, among them the campo datos[dato].bultos I need to add their obtained values.

 for(var dato in datos) {
    var temp = [datos[dato].referencia,datos[dato].estilo,datos[dato].bultos,datos[dato].piezas

_teoricas,datos[dato].total_piezas];
                data.push(temp);

var totales = parseInt(datos[dato].bultos); //Aqui quiero los mis valores como el comentario  var totales = [18,1,153,1,62,1];
// var totales = [18,1,153,1,62,1];
var valor = 0;

                for( var i=0; i < totales.length;i++){
                              valor += totales[i];


                    }
                  console.log(valor);

}

In my var totales I get my values showing it with console.log: var totales = parseInt(datos[dato].bultos);

in my for (var i=0; i < totales.length;i++) I made the process to do the sum and I get the result is 0.

    
asked by Ever 01.03.2018 в 16:56
source

2 answers

2

The values of your json are not of the numerical type, that is why when trying to do the sum returns NAN. To convert them to a numeric type you can:

  

Convert them with the function Number()    let numero = Number("123")

     

Prepend between parentecis operator + , is the equivalent to the   Number () let numero = +("123") function

     

Note The conversion will take place as long as the string only   contain numbers and be free of spaces.

And using your addition function, you could apply with:

for( var i=0; i < totales.length;i++)
    valor += +(totales[i].value);
    
answered by 01.03.2018 в 17:06
2

The error is simple, adding strings instead of numbers, you would need a cast before performing the operation with either parseInt or parseFloat .

According to the format of array it is not correct to access value since it does not have this property, but directly to the element to obtain its value since it is in this part where you can get the error NAN since I would be getting undefined

var totales = ["18","1","153","1","62","1"];
var valor = 0;
for( var i=0; i < totales.length;i++){
    valor += parseInt(totales[i]);
}

console.log(valor);

Another option would be to use reduce to make the addition

var totales = ["18","1","153","1","62","1"];
let suma = totales.reduce((ant,act)=> { return parseInt(ant)+parseInt(act) })
console.log(suma);
  

I perform the process to perform the sum and I get as a result   0.

The parseInt () function receives a string, not an array, by parameter , which when doing parseInt(array) , will only convert the first element and that will be returned

var totales = parseInt(["18","1","153","1","62","1"]);
console.log(totales);

Therefore totales.length will always be undefined since the numeric values do not have this property, and will never enter the for

Please follow the first example of my answer and it will work, cast each iterated element in the for.

    
answered by 01.03.2018 в 17:07