javascript forEach 方法有什么用(那个地图做不到)? [英] what use does the javascript forEach method have (that map can't do)?
问题描述
我在 map 和 foreach 中看到的唯一区别是 map
返回一个数组而 forEach
不是.但是,我什至不明白 forEach
方法的最后一行func.call(scope, this[i], i, this);
".例如,this
"和scope
"不是指同一个对象,也不是 this[i]
和 i
指的是循环中的当前值?
The only difference I see in map and foreach is that map
is returning an array and forEach
is not. However, I don't even understand the last line of the forEach
method "func.call(scope, this[i], i, this);
". For example, isn't "this
" and "scope
" referring to same object and isn't this[i]
and i
referring to the current value in the loop?
我在另一篇文章中注意到有人说当您想根据列表的每个元素执行某些操作时,请使用 forEach
.例如,您可能正在向页面添加内容.本质上,当您想要副作用"时,它非常有用.我不知道副作用是什么意思.
I noticed on another post someone said "Use forEach
when you want to do something on the basis of each element of the list. You might be adding things to the page, for example. Essentially, it's great for when you want "side effects". I don't know what is meant by side effects.
Array.prototype.map = function(fnc) {
var a = new Array(this.length);
for (var i = 0; i < this.length; i++) {
a[i] = fnc(this[i]);
}
return a;
}
Array.prototype.forEach = function(func, scope) {
scope = scope || this;
for (var i = 0, l = this.length; i < l; i++) {
func.call(scope, this[i], i, this);
}
}
最后,除了像这样操作数字之外,这些方法在 javascript 中是否有任何实际用途(因为我们没有更新数据库):
Finally, are there any real uses for these methods in javascript (since we aren't updating a database) other than to manipulate numbers like this:
alert([1,2,3,4].map(function(x){ return x + 1})); //this is the only example I ever see of map in javascript.
感谢您的回复.
推荐答案
在你的例子中 map
和 forEach
的本质区别在于 forEach
> 对原始数组元素进行操作,而 map
作为结果显式返回一个新数组.
The essential difference between map
and forEach
in your example is that forEach
operates on the original array elements, whereas map
explicitly returns a new array as a result.
使用 forEach
,您可以对原始数组中的每个元素进行一些操作,并且可以选择更改.forEach
方法运行您为每个元素提供的函数,但不返回任何内容 (undefined
).另一方面,map
遍历数组,对每个元素应用一个函数,然后将结果作为新数组发出.
With forEach
you are taking some action with -- and optionally changing -- each element in the original array. The forEach
method runs the function you provide for each element, but returns nothing (undefined
). On the other hand, map
walks through the array, applies a function to each element, and emits the result as a new array.
forEach
的副作用"是原始数组被更改.map
的无副作用"意味着,在惯用用法中,原始数组元素不改变;新数组是原始数组中每个元素的一对一映射——映射转换是您提供的函数.
The "side effect" with forEach
is that the original array is being changed. "No side effect" with map
means that, in idiomatic usage, the original array elements are not changed; the new array is a one-to-one mapping of each element in the original array -- the mapping transform being your provided function.
不涉及数据库的事实并不意味着您不必对数据结构进行操作,毕竟这是任何语言编程的本质之一.至于你的最后一个问题,你的数组不仅可以包含数字,还可以包含对象、字符串、函数等.
The fact that there's no database involved does not mean that you won't have to operate on data structures, which, after all, is one of the essences of programming in any language. As for your last question, your array can contain not only numbers, but objects, strings, functions, etc.
这篇关于javascript forEach 方法有什么用(那个地图做不到)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!