1. Method description, the array of reduce () to a function in this array of [X1, x2, x3 ...] , this function must receive two parameters, reduce () to continue the result and the next element of the sequence to do the cumulative calculation, the effect is:
[X1, x2, X3, x4].reduce (f) = f (f (f (x1, x2), x3), x4)
2. Using the example
' 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 pass! ');
}
}
else {
alert (' Test failed! ');
}
The above is a small set to introduce the JavaScript in the use of the reduced () method of using the sample detailed, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!