0

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);
      }
    }
  });
}); 

0

4 Answers 4

2

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));

Sign up to request clarification or add additional context in comments.

6 Comments

Use Object.values(hash_sum) or Object.keys(hash_sum) depending on whether you want an array of names or values.
hello your answer helped me a lot, I would like to know some way to be able to traverse the array type object
you can use what @GirkovArpa mentioned
I have added to my answer both the approaches @DavidRobles
|
2

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 }

Comments

1

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)

Comments

0
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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.