2016-02-23 64 views
-2

有人能请深入向我解释forEach如何在JavaScript中工作,它做了什么,以及如何使用它?我所知道的是它是一种使用函数和数组的循环。我了解其他回路,如for,while,do-whilefor-in,但我只是不明白forEach循环。我尝试了其他在线资源,但我仍然迷失了方向,不知道它的作用以及如何正确“创建”一个,因为我不明白语法。一些帮助将不胜感激。谢谢forEach in JavaScript

+9

[forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) – Tushar

+2

[polyfill](https://developer.mozilla.org)/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Polyfill)@Tushar提供的应该说明一切 - 它是为旧版浏览器编写的,所以应该使用简单的javascript –

+0

@ JaromandaX-自* forEach *在ES5中引入,它只提供符合Ed的pollyfill是有意义的。 3个功能。 ;-) – RobG

回答

-1

for each ... in声明已作为ECMA-357(E4X)标准的一部分被弃用。 E4X支持已被删除,但由于向后兼容性的考虑,每个...都不会被禁用和删除。考虑使用for ...来代替。

实施例:

var sum = 0; 
var obj = {prop1: 5, prop2: 13, prop3: 8}; /* obj array with properties and values */ 

for each (var item in obj) { 
sum += item; 
} 

console.log(sum); 

日志 “26”,这是5 + 13 + 8这里变种将遍历对象的支柱值说,5,13,​​8 ...等,,。

java脚本中这些类型循环的一个重要用途是您不必关心数组或对象数组的索引,无论它是数字还是字符串,循环都会迭代!这在迭代对象时非常有用。

+0

[@ user5934157](http://stackoverflow.com/a/35569280/ 3434588)我相信OP正在寻找'Array.prototype.forEach'函数。 –

+0

不确定ECMA-357与此处相关,* javascript *标记用于EMCA-262又名ECMAScript。 – RobG

1

Array.prototype.forEach是一个函数,它循环访问数组中的所有项,执行定义在第一个参数中的回调函数。语法如下所示:

Array.forEach(function(currentValue, index, array) {}, thisArg); 

函数的内部是什么代码被执行,并且是回调函数的参数如下:currentValue, currentIndex, arrayThatWasCalledOn

它一般是通过一个长用于循环阵列。 第二个参数是thisArgument,此参数设置为的任何值都是该函数范围内相应的this关键字。

Array.forEach(function() {console.log(this.name);}, {name: "value"}); 

请注意,一旦循环开始,在break语句不能用来退出循环。这就是为什么建议使用for循环的原因。

下面是一个代码示例:

var fruits = ["lemon", "apple", "orange", "lettuce"]; 
fruits.forEach(function(currentFruit) { 
    if (isFruit(currentFruit) === false) { 
    console.log("Error: Item is not a fruit!"); 
    } 
} 
+0

你错过了可选的* thisArg *参数。 ;-) – RobG

+0

[@RobG](http://stackoverflow.com/questions/35569115/foreach-in-javascript/35569289#comment58826498_35569289)*可选* –

+0

因此“* ... **可选** thisArg参数... *”。 ;-) – RobG

0

理解在foreach方法从头创建它的最好方法。 forEach本地Javascript函数基本上是一个函数,它内部有一个循环,并将使用迭代器函数(回调函数)来执行集合中的每个项目(集合可能是数组或对象)。

例:

var forEach = function(collection, iterator) {  
    if (Array.isArray(collection)) { 
     for (var i = 0; i < collection.length; i++) { 
     iterator(collection[i], i, collection); 
     } 
    } else { 
     for (var key in collection) { 
     iterator(collection[key], key, collection); 
     } 
    } 
    }; 

现在,如果你调用在foreach功能通过收集和迭代器,它应该从收集记录每名:

// This is your collection 
    var names = ['John', 'Robert', 'James']; 

    // This is the action that you want to perform on each item 
    var action = function(item) { 
    console.log(item); 
    } 

    // Now you execute the forEach function passing the collection and the iterator and it should log each name from the collection. 
    forEach(names, action); // Logs -> 'John' 'Robert' 'James' 

本地的forEach之间的主要区别函数和我们刚刚创建的forEach函数是,您不需要将集合传递给它,因为Javascript将使用实际对象(名称)作为您将迭代的集合。

names.forEach(action); // Logs -> 'John' 'Robert' 'James' 

要在这个问题潜入深,我建议你阅读从Eloquent Javascript book

函数式编程章我希望它能帮助。