Javascript:两个数组相乘和求和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6866196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: Multiply and Sum Two Arrays
提问by CaffGeek
I have two arrays of equal length, and I need to multiply the corresponding (by index) values in each, and sum the results.
我有两个长度相等的数组,我需要将每个数组中相应的(按索引)值相乘,然后对结果求和。
For example
例如
var arr1 = [2,3,4,5];
var arr2 = [4,3,3,1];
would result in 34 (4*2+3*3+4*3+5*1).
将导致 34 (4*2+3*3+4*3+5*1)。
What's the simplest to read way to write this?
写这个最简单的阅读方式是什么?
回答by Buck
var arr1 = [2,3,4,5];
var arr2 = [4,3,3,1];
console.log(arr1.reduce(function(r,a,i){return r+a*arr2[i]},0));
34
This shows the "functional" approach rather than the "imperative" approach for calculating the dot product of two vectors. Functional approach (which tends to be more concise) is preferred in such a simple function implementation as requested by the OP.
这显示了用于计算两个向量的点积的“功能”方法而不是“命令式”方法。在 OP 要求的如此简单的函数实现中,函数方法(往往更简洁)是首选。
回答by Ryan Doherty
var sum = 0;
for(var i=0; i< arr1.length; i++) {
sum += arr1[i]*arr2[i];
}
回答by Endophage
Other answers are almost certainly more efficient, but just to give you a recursive viewpoint (it's nicer in some other languages). It does assume the two arrays are of equal length as you didn't specify what to do if they're not.
其他答案几乎肯定更有效,但只是为了给您一个递归的观点(在其他一些语言中更好)。它确实假设两个数组的长度相等,因为您没有指定如果它们不相等要做什么。
function sumProducts(array1, array2) {
if(array1.length)
return array1.pop() * array2.pop() + sumProducts(array1, array2);
return 0;
}
Edit:
编辑:
katspaugh suggested flipping the returns which is ever so slightly more efficient (don't have to !
the length).
katspaugh 建议翻转返回的效率,这样效率更高(不必太长!
)。
回答by Suhail Ansari
回答by Chris Baker
var arr1 = [2,3,4,5];
var arr2 = [4,3,3,1];
var result = 0;
for (var i=0; i < arr1.length; i++) {
result += (arr1[i] * arr2[i]);
}
alert(result);
Try it here: http://jsfiddle.net/VQKPt/
在这里试试:http: //jsfiddle.net/VQKPt/
回答by Paul
var i, result = 0;
for(i = 0; i < arr1.length; i++)
result += arr1[i]*arr2[i];
alert(result);
Not that this will cause an error if arr2 is shorter than arr1, but you said they're equal length, so I didn't bother checking for it.
并不是说如果 arr2 比 arr1 短,这会导致错误,但是您说它们的长度相等,所以我没有费心检查它。
回答by FishBasketGordo
My vote for simplest-to-read way to write this goes to the humble for loop:
我对最简单易读的方式的投票投给了不起眼的 for 循环:
var ii, sumOfProds = 0;
for (ii = 0; ii < arr1.length && ii < arr2.length; ii++) {
sumOfProds += arr1[ii] * arr2[ii];
}
回答by vandre
This seems pretty straight forward to me
这对我来说似乎很直接
var result=0;
for (var i=0; i<arr1.length;i++){
result+=arr1[i]*arr2[i];
}
回答by Mrchief
Something like this:
像这样的东西:
var sum = 0;
for (var i=0, len = arr1.length; i < len; i++) { // optimized looping
sum += arr1[i] * arr2[i];
}
回答by Prateek patel
function mul (arr1, arr2) {
var n_array = (arr1,arr2).map(x => x * x)
return n_array
}
var a = [1,2,3]
var b = [1,2,3]
console.log(mul(a,b))