我现在正在学习 map() 方法,并且了解非常基本的示例。
var numbers = [2, 4, 6];
var double = numbers.map(function(value) {
return value * 2;
});
我的问题是,开发者在什么情况下使用map()方法来帮助解决问题?有一些好的资源和现实世界的例子吗?
感谢您的帮助!
最佳答案
正如@Tushar 提到的:
The map() method creates a new array with the results of calling a provided function on every element in this array.
因此,当您需要将某些功能应用于数组的每个元素并将结果作为包含新结果的数组返回时,它基本上会被使用。
例如将数字加倍:
var numbers = [1, 4, 9];
var doubles = numbers.map(function(num) {
return num * 2;
});
// doubles is now [2, 8, 18]. numbers is still [1, 4, 9]
关于javascript - javascript 开发人员在什么情况下使用 map() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32471415/