David Robles
David Robles

Reputation: 31

How to sum repeated elements in an array in JavaScript?

I have two arrays. The first is array_impuestos which is the tax names. The second is array_impuestos_valor which contains the corresponding tax values. The tax value for the tax name at index 0 of array_impuestos would be the value at index 0 of array_impuestos_valor.

I need to detect which elements are repeated in array_impuestos and sum the corresponding values in array_impuestos_valor.

For example, 'iva.21%' appears twice in array_impuestos corresponding to both '21' and '10.5' in array_impuesto_valor. I need to sum those last 2 values.

array_impuestos = ["iva.21%", "irfp.-7%", "iva.21%"];
array_impuestos_valor = ["21", "3.5", "10.5"];

array_impuestos.forEach(function (elemento1, indice1, array1) {
  nombre1 = elemento1;
  indice1 = indice1;
  array_impuestos_valor.forEach(function (elemento2, indice2, array2) {
    if (indice1 == indice2) {
      impuesto_repetido = (array_impuestos.indexOf(elemento1) === indice1);
      if (impuesto_repetido == false) {
        //I don't know how to continue sincerely
        console.log(nombre1 + '  ' + elemento2);
      }
    }
  });
}); 

Upvotes: 0

Views: 260

Answers (4)

Syed
Syed

Reputation: 33

let array_impuestos=["iva.21%","irfp.-7%","iva.21%"];
let array_impuestos_valor=["21","3.5","10.5"];

for(let i=0;i<array_impuestos.length;i++){
   console.log(calculation(array_impuestos[i]))
}

function calculation(key){
    let arr = []
    let sum = []
    let total = 0
    array_impuestos.forEach(function(item,index){
        if(item == key) {
            arr.push({key:item,index:index})
        }
    })

for(let item of arr){
    sum.push(array_impuestos_valor[item.index])
}

//performing sum
for(let i=0;i<sum.length;i++){
    total += parseFloat(sum[i])
}
return total
}

Upvotes: 0

namar sood
namar sood

Reputation: 1590

You can do something like this, group the array_impuestos_valor values according to their corresponding array_impuestos values.

let array_impuestos=["iva.21%","irfp.-7%","iva.21%"];
let array_impuestos_valor=["21","3.5","10.5"];

const hash_sum = {};
for(let i=0; i<array_impuestos.length; i++){
    if(!hash_sum[array_impuestos[i]])
    hash_sum[array_impuestos[i]] = +array_impuestos_valor[i];
    else
    hash_sum[array_impuestos[i]] += +array_impuestos_valor[i];
}

console.log(hash_sum);

array_impuestos = [];
array_impuestos_valor = [];

// 1st method
for(const x in hash_sum){
    array_impuestos.push(x);
    array_impuestos_valor.push(hash_sum[x]);
}

console.log(array_impuestos, array_impuestos_valor);

// 2nd method
console.log(Object.keys(hash_sum), Object.values(hash_sum));

Upvotes: 2

GirkovArpa
GirkovArpa

Reputation: 4912

Here's a more functional solution:

const nombres = ['iva.21%', 'irfp.-7%', 'iva.21%'];
const valores = ['21', '3.5', '10.5'];

const impuestos = nombres.reduce((impuestos, nombre, i) => {
  impuestos[nombre] = (impuestos[nombre] || 0) + +valores[i];
  return impuestos;
}, {});

console.log(impuestos); // { 'iva.21%': 31.5, 'irfp.-7%': 3.5 }

Upvotes: 2

Alberto
Alberto

Reputation: 12939

this should work (making use of the concept of object in JS to build a map)

let array_impuestos=["iva.21%","irfp.-7%","iva.21%"];
let array_impuestos_valor=["21","3.5","10.5"];
let res = {};
for(let  i = 0; i < array_impuestos.length; i++)
     res[array_impuestos[i]] = +array_impuestos_valor[i] + (res[array_impuestos[i]] ? res[array_impuestos[i]] : 0)
console.log(res)

Upvotes: 1

Related Questions