javascript forEach方法有什么用途(该地图无法做什么)? [英] what use does the javascript forEach method have (that map can't do)?

查看:92
本文介绍了javascript forEach方法有什么用途(该地图无法做什么)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在map和foreach中看到的唯一区别是, map 正在返回一个数组,而$ code> forEach 不是。但是,我甚至不明白 forEach的最后一行方法 func.call(scope,this [i],i,this) ; 。例如,不是这个范围引用同一个对象,而不是 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆