当前位置 : 主页 > 网络编程 > JavaScript >

JavaScript高阶函数函数1-map

来源:互联网 收集:自由互联 发布时间:2021-06-30
gistfile1.txt /*map/reduce*/'use strict';//求积function product(arr) {return arr.reduce( function(x,y){return x*y;});}//把[1, 3, 5, 7, 9]变换成整数13579var arr = [1, 3, 5, 7, 9];arr.reduce(function (x, y) { return x * 10 + y;});func
gistfile1.txt
/*

map/reduce

*/

'use strict';
//求积
function product(arr) {

return arr.reduce(
    function(x,y){return x*y;});
}
//把[1, 3, 5, 7, 9]变换成整数13579
var arr = [1, 3, 5, 7, 9];
arr.reduce(function (x, y) {
    return x * 10 + y;
});

function string2int(s) {
 return Array.from(s).map(x=>x/1).reduce(function(x,y){return 10*x+y;})
 //return s-0;
}

function normalize(arr) {

}

// parseInt is often used with one argument, but takes two.
// The first is an expression and the second is the radix.
// To the callback function, Array.prototype.map passes 3 arguments: 
// the element, the index, the array
// The third argument is ignored by parseInt, but not the second one,
// hence the possible confusion. 
['1', '2', '3'].map( str => parseInt(str) );
// A simpler way to achieve the above, while avoiding the "gotcha":
['1', '2', '3'].map(Number); // [1, 2, 3]
// but unlike `parseInt` will also return a float or (resolved) exponential notation:
['1.1', '2.2e2', '3e300'].map(Number); // [1.1, 220, 3e+300]
网友评论