Javascript“forEach”循环问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5717409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:06:58  来源:igfitidea点击:

Javascript "forEach" loop question

javascriptjavascript-framework

提问by Mellon

I saw somewhere the code snippet:

我在某处看到了代码片段:

list.forEach(callback, this);

I understand 'forEach' loop except 'this' keyword used here, what does 'this' mean?

我理解 ' forEach' 循环,除了this这里使用的' ' 关键字,' this' 是什么意思?

if I convert list.forEach(callback)to normal for loop, I think it is:

如果我转换list.forEach(callback)为普通的 for 循环,我认为是:

for(var n=0; n<list.length; n++){
   callback(list[n]);
}

But what does 'this' means in forEach(callback, this)? With this, what need to be added if I convert it to normal for loop like above?

但是 ' this' 是什么意思forEach(callback, this)?使用this,如果我将其转换为上面的普通 for 循环需要添加什么?

回答by wong2

thisis used as the this object when executing callback.

this在执行回调时用作 this 对象。

 for(var n=0; n<list.length; n++){
       callback.call(this, list[n]);
    }

see: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/foreach

请参阅:https: //developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/foreach

回答by Karl Laurentius Roos

thisis the current object. The method list.forEach()would be called within object context. You should be able to use the snippet by passing the object you want to loop as the second argument, example:

this是当前对象。该方法list.forEach()将在对象上下文中调用。您应该能够通过将要循环的对象作为第二个参数传递来使用该代码段,例如:

var obj = {
    a: "foo",
    b: "bar"
}

list.forEach(callback, obj);

回答by Michael Berkowski

The forEachis not standard javascript, but probably was part of a library. The Dojo Toolkit has a forEachconstruct like that, or it could have been a method added to the Array()prototype.

forEach不是标准的 javascript,但可能是库的一部分。Dojo Toolkit 具有forEach类似的构造,或者它可能是添加到Array()原型的方法。

The thisargument was most likely an identifier of which object should have context for the callback function. That is, if the callback function itself calls this, which object outside the callback function it should refer to.

this参数最可能是其中对象应具有用于回调函数上下文相关的标识符。也就是说,如果回调函数本身调用this,它应该引用回调函数之外的哪个对象。

回答by simbo1905

There is a forEachon arrays that takes a function which takes a single value which is applied to each element in the collection. A practical example of using a forEach loop is the following which parses the url parameters of the web page:

有一个forEachon 数组,它采用一个函数,该函数采用单个值,该值应用于集合中的每个元素。下面是一个使用 forEach 循环的实际例子,它解析网页的 url 参数:

var query = location.search.slice(1);    
var result = {}, keyValuePairs = query.split('&');

keyValuePairs.forEach(function(keyValuePair) {
    keyValuePair = keyValuePair.split('=');
    result[keyValuePair[0]] = keyValuePair[1] || '';
});

You can then use the resultobject as an associative array or map like structure and get values out of it with var somevalue = result["somekey"]

然后,您可以将该result对象用作关联数组或映射之类的结构,并从中获取值var somevalue = result["somekey"]