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.
-
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
3 Answers
Reset to default 10Use 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
版权声明:本文标题:javascript - How to add two arrays in pairwise fashion - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741321229a2372216.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论