How to add the values of the same arrays positions?

4

I want to add the values of the same positions? Here's my fix:

var arr1 = ["1","2","3","4"];
var arr2 = ["2","1","3","4"];

The result must be:

var NuevoArreglo = ["3","3","6","8"];

I'm waiting for help.

    
asked by Jean Paul 28.06.2016 в 19:56
source

5 answers

3

You can try the following:

var arr1 = [1,2,3,4];
var arr2 = [2,1,3,4];
var r = [];

for(i = 0; i < arr1.length; i++){
  r[i] =arr1[i]+arr2[i];
}

console.log(r);
    
answered by 28.06.2016 / 20:02
source
3

As an alternative, I'll show you how to do it using the Array.prototype.map .

You should know that the map method is used to process an array and obtain another array but transformed by means of a function (which does the transformation); You may be interested to know that it is one of the essential methods to work with fixes, along with Array.prototype.reduce which is used to consolidate / summarize data arrays.

The intention behind map is to transform each element into a new element, with this in mind ... consider the following example.

// si en lugar de cadenas (ej. "1") usas numeros (ej [1,2,3,4])
// puedes eliminar las partes de Number abajo y el map de toString.
var arr1 = ["1", "2", "3", "4"];
var arr2 = ["2", "1", "3", "4"];

// ahora usamos el map
var result = arr1.map(Number)
  .map( (item, ix) => item + Number(arr2[ix]) )
  .map( (item) => item.toString() );

// salida
console.log(result);

Explanation

  • The first .map(Number) convert "1" in 1 (numeric data type)
  • The second .map( (item, ix) => item + Number(arr2[ix]) ) for each number, find and convert the corresponding number of arr2 and add them.
  • The last .map( (item) => item.toString() ) , returns convert each number in a string to get the result.

Note: I have used several maps to better illustrate how they are used, it is not necessary to do it in so many steps, it can be done in a single execution:

arr1.map( (item, ix) => (Number(item) + Number(arr2[ix])).toString() );

Now ..

If instead of strings, you have the numbers right, the thing is much simpler.

// si en lugar de cadenas (ej. "1") usas numeros (ej [1,2,3,4])
// puedes eliminar las partes de Number abajo y el map de toString.
var arr1 = [1, 2, 3, 4];
var arr2 = [2, 1, 3, 4];

var result = arr1.map( (item, ix) => item + arr2[ix] );
// salida
console.log(result);

I hope it serves you.

Note: I find it more readable to use map than to use a loop type for each or similar control structures because it looks like a string of functions and there are not so many brackets. But it's a matter of taste.

    
answered by 28.06.2016 в 23:43
2

Try this way:

var arr1 = ["1","2","3","4"];
var arr2 = ["2","1","3","4"];
var result = [];

for (var i = 0; i < arr1.length; i++) {
  if (arr1[i] != arr2[i] ) {
    result[i] = parseint(arr1[i])+parseint(arr2[i]);
  }
}
console.log(result);
    
answered by 28.06.2016 в 20:09
2

Here I leave you a script where you do the sum using a forEach, this way of doing it is contemplating the new EcmaScript 6.

See the use of arr1.forEach .

var arr1 = ["1","2","3","4"];
var arr2 = ["2","1","3","4"];
var total = new Array(); //Creamos un array Vacio
arr1.forEach((a,b) => { total.push(Number(a)+Number(arr2[b])) } ); //Iteramos el array llamado: arr1. Con el método Number los declaramos enteros y los sumamos
console.log(total);
    
answered by 28.06.2016 в 20:36
0

Perhaps the simplest option is to use Array.map() :

var arr1 = ["1","2","3","4"];
var arr2 = ["2","1","3","4"];

var nuevo = arr1.map((x,i)=> (+x + +arr2[i]) + "");
console.log(nuevo);

According to your question, the initial Arrays contain String s and you want to add them and convert them back to String , according to the expected output that you show in the question.

(+x + +arr2[i]) + ""

Prefixing each variable with the symbol + we are saying to convert the variable to a number. And adding + "" we convert the final number added in String.

Keep in mind that both lists must be the same size.

    
answered by 28.06.2016 в 23:41