# 题目描述
请实现Array.map
函数的功能且该新函数命名为_map
# 测试用例
function c() {
const a = [1, 2, 3];
const newa = a._map((i) => i * 3);
judge = JSON.stringify(newa) === "[3,6,9]";
return judge;
}
1
2
3
4
5
6
2
3
4
5
6
输出:true
# 代码实现
Array.prototype._map = function(cb){
console.log(Object.prototype.toString.call(cb));
if(Object.prototype.toString.call(cb) !== "[object Function]"){
return -1;
}
let arr = this;
for(let i = 0; i < arr.length; i ++){
arr[i] = cb(arr[i], i, arr);
}
return arr;
}
function test() {
const a = [1, 2, 3];
const newa = a._map((i) => i * 3);
judge = JSON.stringify(newa) === "[3,6,9]";
return judge;
}
console.log(test());
// true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23