1.for Cycle
for (var i= 0; i<10; i++) {
Console.log (' This content will print 10 times ')
}
for in
-
For in is not suitable for traversing an array: Because the value of V is not a numeric type but a string type, it can cause great inconvenience in string operations.
-
var myarr = [10,20,40,70];
for (v in myarr) {
console. Log (v); //1
Span class= "cm-variable" >console. log (typeof v); //string
}
?
?
?
+ For more for iterating over arrays
+ Loop out is the value
?
"' JavaScript
var Myarr = [10,20,40,70];
For (v in Myarr) {
Console.log (v); 10
Console.log (typeof v); Num
}
2.forEach ()
VarMyarr= [' 10 ',' 20 ',' 40 ',' 70 '];
VarOtherarr= [' 2 ',' 3 ']
Myarr.ForEach (function ( v,i,) {
Console. log (v) //value value
Console. log (i) //corresponding subscript
console. log (this) //this to the parameters of foreach 2
},otherarr);
3.map () compatibility problem but it's a dick!!
ForEach () and map () Traverse
Common
1. All loops iterate through each item in the array.
Each execution of the anonymous function in 2.forEach () and map () supports 3 parameters: the current item in the array item, the index of the current item, and the original array input.
3. This in the anonymous function refers to window.
4. Only the array can be traversed.
4.some ()
Varage = [4,12,18,20,21];
var judge = age. some (function (v) { //traversal value in age greater than 20 returns ture and returns false
return v>20;
})
console. Log (judge); //true
5.filter ()
VarAge= [4,12,18,20,21,< Span class= "Cm-number" >23,99, ' wo ';
var judge = age. filter (function (v) {
return v>< Span class= "Cm-number" >20;
})
console. Log (judge); [21,23,99]
Character array traversal loop