如何通过Array.prototype.reduce()函数找到以下多维数组中所有数字的总和:
var arr = [["one",3],["five",15],["ten",30],["twenty",40]];
Run Code Online (Sandbox Code Playgroud)
我知道如何使用for循环,但只是想知道......
你可以这样做,
var sum = [["one",3],["five",15],["ten",30],["twenty",40]].reduce(function(a,b){
return a + b[1];
}, 0);
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,作为第二个参数传递的0是要在计算中使用的初始值.
| 归档时间: |
|
| 查看次数: |
727 次 |
| 最近记录: |