-1

I followed an example to use reduce and sum the values of some keys, but now, i want to sum values of an array of objects inside of this reduce...

This is what i have:

groupConceptos(){
        var result = [];                        
        this.auxTablaPrincipal.reduce((res, value, index) => {     
            var aux = [];               
            value.conceptoImpuesto.reduce((ant, nuevo) => {
                if(!ant[nuevo.nombre]){
                    ant[nuevo.nombre] = {
                        base: 0,
                        importe: 0,
                        impuesto: nuevo.impuesto,
                        local: nuevo.local,
                        nombre: nuevo.nombre,
                        tasaOcuota: nuevo.tasaOcuota,
                        tipoFactor: nuevo.tipoFactor,
                        tipoImpuesto: nuevo.tipoImpuesto
                    };
                    aux.push(ant[nuevo.nombre])
                }
                ant[nuevo.nombre].base += parseFloat(nuevo.base);
                ant[nuevo.nombre].importe += parseFloat(nuevo.importe);
                return ant
            }, {});

            if(!res[value.descripcion]){                   
                res[value.descripcion] = {                        
                    conceptoImpuesto: aux,//value.conceptoImpuesto,                        
                    cantidad: 0,
                    claveInterna: value.claveInterna,
                    claveProdServ: value.claveProdServ,
                    claveUnidad: value.claveUnidad,
                    descripcion: value.descripcion,
                    descuento: 0,                        
                    importe: 0,
                    subtotal: 0,
                    porcentajeIva: value.porcentajeIva,
                    precioPublico: value.precioPublico,
                    productoId: value.productoId,
                    sumaImporte: 0, 
                    totalIva: value.totalIva,
                    totalPagado: value.totalPagado,
                    unidad: value.unidad,
                    valorUnitario: value.valorUnitario                                          
                };
                result.push(res[value.descripcion])
            }             
            res[value.descripcion].cantidad += parseInt(value.cantidad);
            res[value.descripcion].descuento += parseInt(value.descuento);
            res[value.descripcion].importe += parseFloat(value.importe); 
            res[value.descripcion].subtotal += parseFloat(value.subtotal);                         
            res[value.descripcion].sumaImporte += parseInt(value.sumaImporte);                                                                                   
            return res
        }, {});

        this.tablaPrincipal = result;  
            this.tablaPrincipal.forEach(element => {
                element.conceptoImpuesto.forEach(concepto => {
                    this.auxConceptosImpuestos.push({
                        base: concepto.base,
                        importe: concepto.importe,
                        impuesto: concepto.impuesto,
                        local: concepto.local,
                        nombre: concepto.nombre,
                        tasaOcuota: concepto.tasaOcuota,
                        tipoFactor: concepto.tipoFactor,
                        tipoImpuesto: concepto.tipoImpuesto
                    })
                });
            });                                       
    },

* this.auxTablaPrincipal is an array that i use to reduce and assign to this.tablePrincipal

this.tablaPrincipal is the result of the reduce of this.auxTablaPrincipal. conceptoImpuesto is an array of objects that i need to sum some values like this: res[value.descripcion].cantidad += parseInt(value.cantidad);

So i use the same method to reduce and sum value.conceptoImpuesto of the reduce of this.auxTablaPrincipal, and then assign aux array to conceptoImpuesto of the this.auxTablaPrincipal

But this method is not working.

How to sum values of an array inside of a reduce method?

EDIT

enter image description here

For example, i use reduce to group values if a new value is the same that its present in the array. So i want to sum values of a the conceptoImpuesto array to apply the sum of the conceptoImpuesto.

In the image the value of "canatidad" is 2. So, the values of "base" and "importe" of the array conceptoImpuesto add up, like i do in the code

Isaías Orozco Toledo
  • 1,919
  • 5
  • 19
  • 35
  • `reduce` returns a value, so you may want to use it (store it into a variable, or return it, ...). – ibrahim mahrir Jan 07 '19 at 17:27
  • 1
    Can you show an example of the array and the expected outcome? – ibrahim mahrir Jan 07 '19 at 17:28
  • @ibrahimmahrir i update the question with an example – Isaías Orozco Toledo Jan 07 '19 at 17:42
  • Possible duplicate of [How to find the sum of an array of numbers](https://stackoverflow.com/questions/1230233/how-to-find-the-sum-of-an-array-of-numbers) – Dexygen Jan 07 '19 at 17:44
  • ... and the expected output? – ibrahim mahrir Jan 07 '19 at 17:53
  • @ibrahimmahrir like the image, with the sum of the values "base" and "importe"... 2.14 is the value "default" when enter the first element in an empty array and when the reduce is active, sum values of "base" and "importe": 2.14 + 2.14... all of this in the same reduce of this.auxTablaPrincipal, I have tried almost everything but i can not sum the values, only the others values outside the array... In "cantidad" was applied the reduce, sum 1 (the first element present in the array) + 1 (the new same element) = 2 – Isaías Orozco Toledo Jan 07 '19 at 19:06

1 Answers1

0

So what you want to do is initialize the reduce with 0 and then add to it from each array item. For example if you have an array like this:

const employees = [
  { name: 'Bob', yearsExperience: 5 },
  { name: 'Juanita', yearsExperience: 10 },
  { name: 'Shen', yearsExperience: 12 }
]

And you want the cumulative teams' experience, you could do this:

employees.reduce((accumulator, value) => {
  accumulator += value.yearsExperience
  return accumulator
}, 0)
Paul
  • 35,689
  • 11
  • 93
  • 122
  • Yes but, the sum of the values it has to be an array – Isaías Orozco Toledo Jan 07 '19 at 19:15
  • ? how is a sum an array? Do you mean you need to do a group by kind of transformation? – Paul Jan 07 '19 at 19:21
  • like in the code in my question... `res[value.descripcion].cantidad += parseInt(value.cantidad);`... Build an array and sum values, but inside of the array there is another array, and this array have values that add up, so, build an array to assign to `conceptoImpuesto` of `this.auxTablaPrincipal`, and finally assign to this.tablaPrincipal this... `this.tablaPrincipal = result`... `result` is the variable with the reduce – Isaías Orozco Toledo Jan 07 '19 at 19:32