js实现map接收参数
实现 Map 接收参数的方法
在 JavaScript 中,map 是数组的一个高阶函数,用于对数组中的每个元素执行指定的操作并返回一个新数组。map 方法可以接收多个参数,以下是详细说明:

map 方法的基本语法
array.map(callback(currentValue[, index[, array]])[, thisArg])
callback:对每个元素执行的函数,接收以下参数:currentValue:当前处理的元素index(可选):当前元素的索引array(可选):调用map的数组本身
thisArg(可选):执行callback时使用的this值
使用示例
const numbers = [1, 2, 3, 4];
// 仅使用 currentValue 参数
const doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6, 8]
// 使用 currentValue 和 index 参数
const withIndex = numbers.map((num, index) => `${index}: ${num}`);
console.log(withIndex); // ["0: 1", "1: 2", "2: 3", "3: 4"]
// 使用 currentValue, index 和 array 参数
const withArray = numbers.map((num, index, arr) => num + arr.length);
console.log(withArray); // [5, 6, 7, 8]
// 使用 thisArg 参数
const obj = { multiplier: 10 };
const withThisArg = numbers.map(function(num) {
return num * this.multiplier;
}, obj);
console.log(withThisArg); // [10, 20, 30, 40]
注意事项
map不会改变原数组,而是返回一个新数组map会跳过空元素,但不会跳过值为undefined或null的元素- 如果未提供
thisArg,在严格模式下this为undefined,非严格模式下为全局对象






