JavaScript 中的 map() 数组方法

比在数组上使用循环更凉爽的方式,map()在数组上迭代并创建一个新的数组,每个项目都通过了一个函数:

1var drinks = [coffee soda, tea, whiskey];
2
3var coldDrinks = drinks.map(function(drink) {
4    return iced  + drink;
5});
6
7// [‘iced coffee soda’, ‘iced coffee’, ‘iced whiskey’]

语法非常简单:

1var newArray = myArray.map(function);
Published At
Categories with 技术
Tagged with
comments powered by Disqus