javascript的数组之map()

map()方法创建一个新数组,其结果是该数组中的每个元素都调用一个提供的回调函数后返回的结果。新数组

 1 // ES6
 2 let numbers = [1, 5, 10, 15];
 3 let doubles = numbers.map( x => x ** 2);
 4 
 5 // doubles is now [1, 25, 100, 225]
 6 // numbers is still [1, 5, 10, 15]
 7 
 8 
 9 const numbers = [2, 4, 8, 10];
10 let halves = numbers.map(x => x / 2);
11 
12 let numbers = [1, 4, 9];
13 let roots = numbers.map(Math.sqrt);
14 // roots is now [1, 2, 3]
15 // numbers is still [1, 4, 9]

参数:

第一个:回调函数,生成新数组元素

第二个:传入回到函数的this值

猜你喜欢

转载自www.cnblogs.com/huanqiuxuexiji/p/9167856.html