1. Method description, Array's reduce() applies a function to the [x1, x2, x3...] of this Array. This function must receive two parameters. Reduce() continues the result to perform a cumulative calculation with the next element of the sequence. The effect is:
[x1, x2, x3, x4].reduce(f) = f(f(f(x1, x2), x3), x4)
2. Use examples
'use strict';function string2int(s){if(!s){alert('the params empty');return;}if(s.length===1){return s*1;}var arr = [];for(var i=0; i<s.length; i++){arr.push(s.substr(i, 1)*1);}return arr.reduce(function(x, y){return x*10 + y;});}// Test: if (string2int('0') === 0 && string2int('12345') === 12345 && string2int('12300') === 12300) {if (string2int.toString().indexOf('parseInt') !== -1) {alert('Do not use parseInt()!');} else if (string2int.toString().indexOf('Number') !== -1) {alert('Do not use Number()!');} else {alert('Test passed!');}}else {alert('Test failed!');}The above is a detailed explanation of the use example of the reduce() method included in JavaScript introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!