Loops and iterations of JavaScript arrays
Cycle
For loop
Normal version for loop
var arr = ["a","b","C", " D " ]; for 0; i < arr.length; i++) { console.log (arr[i]); . // a b c d}
Optimized for Loop
var arr = ["a","b","C", " D " ]; for 0; Len = Arr.length;i < len; i++) { console.log (arr[i]); . // a b c d}
For In loop
var arr = ["a","b","C", " D " ]; for inch arr) { console.log (arr[i]); . // a b c d}
For of Loops
var arr = ["a","b","C", " D " ]; for (Let item of arr) { console.log (item); . // a b c d}
Description
- For-in is the key name that iterates through the collection object, and for-of is the key value that traverses the collection object directly.
- For-of has a better traversal performance than for-in, but it is still more than normal for loop.
- For-of not only supports array traversal, but also supports most class array objects. also supports new MAP and Set objects in ES6
Array iterations
ForEach ()
Description: Sequentially enables each item in the array to perform some of the same operations sequentially.
var arr = [ten,];arr.foreach (The function (item, Index ) { Console.log (item); // console.log (index); // 0 1 2) 3 4})
Description
- The array comes with a ForEach loop that uses a higher frequency but is actually weaker than the normal for loop.
ForEach cannot use the continue and break statement to interrupt a loop, nor can it return to an outer function using a return statement sentence.
Map ()
Description: Sequentially let each item in the array perform some of the same operation sequentially, and then return a new array
var arr = [tento one]; var newArr = arr.map (function (item, index) { return2;}) Console.log (NEWARR); // [26,28]
Filter ()
Description: Sequentially let each item in the array perform some of the same operations sequentially, then filter out the elements that match the condition, make up a new array, and return.
var arr = [tento one]; var newArr = arr.filter (function (item, index) { return20 ; }) Console.log (NEWARR); // [Ten, A.]
Feduce ()
Description: Make a calculation of the preceding and latter in the array sequentially, and return the final value cumulatively.
var arr = [tento one]; var result = arr.reduce (function (prev, next) { return prev + Next;}) Console.log (result); // -
Every ()
Description: Each item in the array is sequentially followed by a similar operation to detect whether each item in the array meets the criteria.
var arr = [tento one]; var result = arr.every (function (item, index) { return20 ; }) Console.log (result); // false
Description: A detected element that returns true if all conditions are met, false if one of the items does not meet the criteria. That is, a false must leave.
Some ()
Description: Each item in the array is sequentially executed in one of the same actions to detect whether an item in the array is eligible.
var arr = [tento one]; var result = arr.some (function (item, index) { return20;}) Console.log (result); // True
Description: In the instrumented element, if one of the items matches the condition, the result is true and the result is false if none of the conditions are met. That one will be true.
Loops and iterations of JavaScript arrays