the problem is the following: I have 2 array
that I get automatically, which I must do the subtraction between the first and the second but for example position:
var array1 = [9,8,7,6,5]
var array2 = [10,20,30,40,50]
// (9-10),(8-20).......
/* objetivo
var array_resta = [-1,-12,-23,-34,-45]*/
//lo que había hecho yo era, meter los 2 array en otro array algo así :
var receptor = new Array();
receptor.push(array1)
receptor.push(array2)
// receptor = [[9,8,7,6,5],[10,20,30,40,50]]
// para luego intentar restar algo así:
let a_resta = new Array();
for (j = 0; j < 5; j++) {
let resta = 0;
for (i = 0; i < receptor.length; i++) {
// if (receptor[i][j] > 0) { //esto solo es una condicion por si uno de los array es de > length
resta -= receptor[i][j];
// }
}
a_resta.push(resta);
}
console.log(a_resta);
// lo unico que hace es hacer la suma y mostrar en negativo(-)
Well then, this is what I have, I'm blank, I don't know if it's a good idea to do the "put" in a array
"general" to try to do what I need.
First we iterate through any of the two arrays (since both should have the same length) and then we transform values using
map
, beingr
the current element of the array traversed andi
the numerical index, so if there are numbers to compare we subtractarray1[i] - r
which is fair what you need, in case the arrays differ in size we make sure to have a backup, making the result value the second array without subtracting.I recommend that you use the Array prototype function
reduce
( from the Vanilla JavaScript build-in ).The function
reduce
is a property, therefore, of each array . It takes as its first argument a callback that will be executed for each element of the array and, optionally, an initial value.In your case, your function must return an array with the rest of the elements. For this reason, the initial value that we will pass as an argument will be an empty array
[]
, to which we are going to insert the result of the subtractions.The callback that takes the function as argument
reduce
will receive the accumulated value (in our case it will be the result array ), the current element of the iteration and, optionally, the index and the original array .The code can really be drastically reduced using the spread operator .
In the same subtraction you can add the logic of what happens when these two arrays do not have the same length. You should also take into account the order in which you pass the arrays to the same function.
EDIT: Extend the case of two arrays with different length.
First, let's add an abstraction layer to the function,
merge
will result in an array based on two arrays passed by argument. The parameteroperation
will be in charge of the calculation and the logic of each element based on the two elements in the same position of both array .With that, we refactor the function from before without taking into account that two arrays can have different sizes.
With the abstraction, we can define several ways to get the function
substract
.In this case, the smallest array positions will only be filled with the largest array element at that position.
I hope it works.