We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
forEach
map
item(当前项)
index(索引值)
arr(原数组)
this
window
map()方法返回一个新的数组, 数组中的元素为原始数组调用函数处理后的值.
map()
map()方法不会改变原始数组
map()方法不会对空数组进行检测
forEach方法用于调用数组的每个元素, 将元素传给回调函数(没有return, 返回值是 undefined)
forEach()对于空数组是不会调用回调函数的
forEach()
var arr1 = [1,2,3,4] var arr2 = arr1.map(item => item+1) var arr3 = arr1.forEach(item => item * 2) var arr_empty_1 = [] var arr_empty_2 = arr_empty_1.map(item => item+1) console.info(arr1) // [1,2,3,4] console.info(arr2) // [1,2,3,4] console.info(arr3) // undefined console.info(arr_empty_1) // [] console.info(arr_empty_2) // []
The text was updated successfully, but these errors were encountered:
No branches or pull requests
JavaScript: forEach 和 map
相同点
forEach
和map
方法里每次执行匿名函数都支持3个参数:item(当前项)
,index(索引值)
,arr(原数组)
需要用哪个的时候就写哪个, 匿名函数中this
都是指向window
不同点
map()
方法返回一个新的数组, 数组中的元素为原始数组调用函数处理后的值.map()
方法不会改变原始数组map()
方法不会对空数组进行检测forEach
方法用于调用数组的每个元素, 将元素传给回调函数(没有return, 返回值是 undefined)forEach()
对于空数组是不会调用回调函数的The text was updated successfully, but these errors were encountered: