- map
map()方法定义在JavaScript的Array中,调用Array的map()方法,传入我们自己的函数,就可以得到结果,来一个例子
'use strict';
function pow(x) {
return x * x;
}
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var results = arr.map(pow); // [1, 4, 9, 16, 25, 36, 49, 64, 81]
console.log(results);
map()还可以干点别的,比如:
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
arr.map(String); // ['1', '2', '3', '4', '5', '6', '7', '8', '9']
把Array所有数字转换为字符串
reduce
再看reduce的用法。A