js在開發過程中遇到非常多的循環遍歷,如for, for in, for of, forEach, filter, each, every, some, map…
1、for循環
語法
for ( init; condition; increment ){
statement(s);
}
特點
- 可以退出循環:break和continue;
- 循環代碼塊一定的次數;
2、for in循環
語法
for (var val in list) {
//語句
}
特點
- val 需要爲 string 或 any 類型;
- for/in 語句循環遍歷對象的屬性;
3、while循環
4、do while循環
5、forEach() 方法
是Array對象的一個方法
還有 some(), every(), map(),filter(),entries(), find(), findIndex(), keys()
語法
array.forEach(function(currentValue, index, arr), thisValue)
參數說明:
- function(currentValue, index, arr): 必需。 數組中每個元素需要調用的函數。函數參數說明:
- currentValue:必需。當前元素
- index:可選。當前元素的索引值。
- arr:可選。當前元素所屬的數組對象。
- thisValue:可選。傳遞給函數的值一般用 “this” 值。如果這個參數爲空, “undefined” 會傳遞給 “this” 值
6、 for of循環
ES6中的循環,可以遍歷Arrays,Strings, Maps, Sets等可迭代的數據結構。
7、ES6中新增的遍歷
待續、、、
8、each()
JQ中的循環