有人能請深入向我解釋forEach
如何在JavaScript中工作,它做了什麼,以及如何使用它?我所知道的是它是一種使用函數和數組的循環。我瞭解其他迴路,如for
,while
,do-while
和for-in
,但我只是不明白forEach
循環。我嘗試了其他在線資源,但我仍然迷失了方向,不知道它的作用以及如何正確「創建」一個,因爲我不明白語法。一些幫助將不勝感激。謝謝forEach in JavaScript
回答
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腳本中這些類型循環的一個重要用途是您不必關心數組或對象數組的索引,無論它是數字還是字符串,循環都會迭代!這在迭代對象時非常有用。
[@ user5934157](http://stackoverflow.com/a/35569280/ 3434588)我相信OP正在尋找'Array.prototype.forEach'函數。 –
不確定ECMA-357與此處相關,* javascript *標記用於EMCA-262又名ECMAScript。 – RobG
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!");
}
}
理解在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
函數式編程章我希望它能幫助。
- 1. Foreach in foreach [PHP]
- 2. Powershell foreach in pssession
- 3. forEach in angular
- 4. AS3 dispatchEvent in forEach
- 5. foreach:「in」v。「as」
- 6. Foreach in Linq
- 7. FOREACH in cypher - neo4j
- 8. Java逗號「,」in forEach
- 9. Laravel-5 Foreach in blade
- 10. Formignal in foreach循環codeigniter
- 11. 併發地圖/ foreach in scala
- 12. foreach而不是FOR IN R
- 13. Foreach vs list-each in PHP
- 14. foreach in form not working properly
- 15. PHP preg_replace in javascript
- 16. JavaScript - ForEach
- 17. ()[] +! in javascript
- 18. Javascript` this` in objects-in-objects?
- 19. SetInterval with javascript in javascript
- 20. 的Javascript的foreach
- 21. Javascript:錯誤forEach
- 22. JavaScript foreach onclick
- 23. Javascript Foreach Function Expected
- 24. 何時使用forEach(_ :)而不是in?
- 25. jQuery foreach check in tr> first td
- 26. 「(key in obj)」還是「.forEach」更快?
- 27. sql查詢:使用哪裏in或foreach?
- 28. for-in循環到for循環或forEach
- 29. Foreach in List <t> Facebook好友
- 30. 確實foreach(<$IN>)sl the文件?
[forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) – Tushar
[polyfill](https://developer.mozilla.org)/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Polyfill)@Tushar提供的應該說明一切 - 它是爲舊版瀏覽器編寫的,所以應該使用簡單的javascript –
@ JaromandaX-自* forEach *在ES5中引入,它只提供符合Ed的pollyfill是有意義的。 3個功能。 ;-) – RobG