JavaScript Array#map:index参数 [英] JavaScript Array#map: index argument
问题描述
map
方法。 你可以传递一个函数,采取第二个参数,正在处理的数组的当前元素的索引,但...到什么目的?当你不这样做会发生什么,当你没有时会有什么不同?
你将会使用这个功能?
当前项目的索引总是传递给回调函数,唯一的区别,如果你不在函数中声明它是你不能访问它
示例:
[1,2,3] .map(function(o,i){
console.log(i);
return 0;
});
[1,2,3] .map(function(o){
console.log(arguments [1]); //它还在
return 0;
});
输出:
code> 0
1
2
0
1
2
演示: http://jsfiddle.net/Guffa/k4x5vfzj/
My question is about the map
method of arrays in JavaScript.
You can pass it a function that takes a second argument, the index of the current element of the array being processed, but... to what purpose? What happens when you do it and what's the difference when you don't?
What would you use this feature for?
The index of the current item is always passed to the callback function, the only difference if you don't declare it in the function is that you can't access it by name.
Example:
[1,2,3].map(function(o, i){
console.log(i);
return 0;
});
[1,2,3].map(function(o){
console.log(arguments[1]); // it's still there
return 0;
});
Output:
0
1
2
0
1
2
Demo: http://jsfiddle.net/Guffa/k4x5vfzj/
这篇关于JavaScript Array#map:index参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!