How can I find the sum of all the numbers in the following multidimensional array by the Array.prototype.reduce() function:
var arr = [["one",3],["five",15],["ten",30],["twenty",40]];
I know how to do that using for loop, but just wondering...
You can do it like this,
var sum = [["one",3],["five",15],["ten",30],["twenty",40]].reduce(function(a,b){
return a + b[1];
}, 0);
In the above code, 0 passed as a second argument is the initial value to be used in the calculation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With