admin管理员组

文章数量:1287708

I would like to add the values of two JavaScript arrays that have the same length to get a third array so that the the first value of the third array is the sum of the first values of the two first arrays, the second value of the third array is the sum of the second values of the two first arrays, etc. For example:

var array1 = [1,2,3];
var array2 = [4,1,0];
var array3 = array1 + array2;

I would like the result of array3 to be [1+4, 2+1, 3+0] = [5,3,3].

This is not the same question as this. I would like to add the numbers and not make sub-arrays.

I know that you can do for(i = 0; i < array1.length; i++){array3[i] = array1[i] + array2[i]} but I would like to know if there is a built-in code that does this.

I would like to add the values of two JavaScript arrays that have the same length to get a third array so that the the first value of the third array is the sum of the first values of the two first arrays, the second value of the third array is the sum of the second values of the two first arrays, etc. For example:

var array1 = [1,2,3];
var array2 = [4,1,0];
var array3 = array1 + array2;

I would like the result of array3 to be [1+4, 2+1, 3+0] = [5,3,3].

This is not the same question as this. I would like to add the numbers and not make sub-arrays.

I know that you can do for(i = 0; i < array1.length; i++){array3[i] = array1[i] + array2[i]} but I would like to know if there is a built-in code that does this.

Share Improve this question edited May 23, 2017 at 12:15 CommunityBot 11 silver badge asked Jun 2, 2016 at 12:30 Donald Duck is with UkraineDonald Duck is with Ukraine 8,90223 gold badges79 silver badges102 bronze badges 4
  • 1 array1.map((e, i) => e + array2[i]) – Tushar Commented Jun 2, 2016 at 12:32
  • Possible duplicate of Javascript equivalent of Python's zip function – Rhumborl Commented Jun 2, 2016 at 12:34
  • @Rhumborl I would like to add the numbers and not make sub-arrays. – Donald Duck is with Ukraine Commented Jun 2, 2016 at 12:44
  • Why do you consider the for loop a workaround? It's just a bit of code that does what you want. The answer to your question, is no, there is no "built-in code that does this". – user663031 Commented Jun 2, 2016 at 12:59
Add a ment  | 

3 Answers 3

Reset to default 10

Use Array#map() method

var array1 = [1, 2, 3];
var array2 = [4, 1, 0];
var array3 = array1.map(function(v, i) {
  return v + array2[i];
})

console.log(array3);


For latest browser use it with ES6 arrow function

var array1 = [1, 2, 3];
var array2 = [4, 1, 0];
var array3 = array1.map((v, i) => v + array2[i])

console.log(array3);


For older browser check polyfill option of map method.

You should use generators.

function *addPairwise(a1, a2) {
  let i1 = a1[Symbol.iterator](), i2 = a2[Symbol.iterator](), x1, x2;

  while (1) {
    x1 = i1.next();
    x2 = i2.next();
    if (x1.done && x2.done) return;
    yield x1.done ? x2.value : x2.done ? x1.value : x1.value + x2.value;
  }
}

Normally we would prefer to simply do a for...of loop over an iterable, but there's no way to do that in parallel over two iterables, so we need to get the iterators so we can use the next method on them.

This approach will allow you to do pairwise addition of any iterable including infinite streams.

console.log([...addPairwise([1,2,3], [4,1,0])]);

If you prefer you could define addPairwise to take iterators directly and call it as

addPairwise([1, 2, 3].values(), [4, 1, 0].values())

This assumes suitable support for ES6 such as node v6, Chrome 51, or babel.

var array1 = [1,2,3];
var array2 = [4,1,0];

var array3 = add(array1, array2);
console.log(array3);

function add(arr1, arr2) {
  return arr1.map(function(value, index) {
    return value + arr2[index];
  });
}

For IE8 support:

var array1 = [1,2,3];
var array2 = [4,1,0];

var array3 = add(array1, array2);
console.log(array3);

function add(arr1, arr2) {
  var newArray = [];
  
  for(var i = 0; i < arr1.length; i++){
    newArray.push(arr1[i] + arr2[i]);
  }
  
  return newArray;
}

本文标签: javascriptHow to add two arrays in pairwise fashionStack Overflow