1. For loop

Var size=[1,2,3,4,5,6,7] for(var I =0, len=size.length; i<len; i++){ document.write(size[i] + " "); // Array element}Copy the code

2. For in can loop through arrays and objectsUse "for in" when recommending objects

var x; // We can not define, Var obj={name:'liushenghua',age:24,sex:'man'} // Declare an object for(x in size){ document.write(size[x] + " "); } for(x in obj){document.write(obj[x] + ""); // Object element}Copy the code

3, for… Of is a new feature introduced in ES6. It is simpler than traditional for loops, but it makes up for forEach and for-in loops. For of can’t loop over ordinary objects,for in can loop over custom attributes, and for of can’tUse "for of" when recommending arrays

Var size=[1,2,3,4,5,6,7] {document.write(value==7? value + "--":value + " "); For (let value of size){document.write(value==7? value + "--":value + " "); For (const value of size){document.write(value + ""); // Loop a string let iterable = "boo"; for (let value of iterable) { console.log(value); } // But you can loop in an object that has an Enumerable property. Var obj = {name: 'zhangshan', age: 23, color: 'red'} for (var item of object.keys (obj)) {console.log(item)}  var arr = ['apple', 'banana', 'orange'] arr.name = 'name'; For (var key in arr) {console.log(arr[key])// Apple banana orange name} for (var item of arr) { Console. log(item)//apple banana orange grape} // for of will not skip undefined values,for in will skip undefined values as follows:  var arr = [, 'apple', 'banana', 'orange'] for (var key in arr) { console.log(arr[key])//apple banana orange } // console.log('--------') for (var item of arr) { console.log(item)//undefined apple banana orange }Copy the code

4. The for loop and the while loop are interchangeable

while

cars=["BMW","Volvo","Saab","Ford"];
var i=0;
while (cars[i]){
	document.write(cars[i] + "<br>");
	i++;
}
Copy the code

The for loop

cars=["BMW","Volvo","Saab","Ford"]; var i=0; for (; cars[i];) { document.write(cars[i] + "<br>"); i++; }Copy the code

5, The difference between for and for in

When an array is defined, we assign to the array. If some subscripts are not used (i.e., not assigned), we use the general for loop and for… In loops give different results. for… The in loop automatically skips unassigned elements, while the for loop does not. It displays undefined.

<p> Click the button below, </p> <button onclick="myFunction()"> </p> <script> function myFunction(){var array = new Array(); var x; var txt="" array[0] = 1; array[3] = 2; array[4] = 3; array[10] = 4; for( x in array ){ alert(array[x]); } alert(array.length); For (var I =0; i<4 ; i++){ alert(array[i]); } document.getelementById ("demo"). InnerHTML = TXT; } </script>Copy the code

A for loop can break out of the loop at any time. If there is only one curly brace, continue skips the break step without the curly brace

for (i=0; i<10; I ++) {if (I ==3) break; x=x + "The number is " + i + "<br>"; }Copy the code

continue

for (i=0; i<10; I ++) {if (I ==3) continue; x=x + "The number is " + i + "<br>"; }Copy the code

6. ForEach method

A concept,

The forEach() method performs a callback forEach valid item in the array in ascending order, and those deleted or uninitialized items are skipped (for example, on a sparse array).

ForEach () does not execute a callback for an empty array. There is no way to abort or exit the forEach() loop except by throwing an exception

Second, the grammar

Arr. forEach(callback(currentValue, index, arr), thisArg)

Callback: Mandatory. A function executed for each element in the array that takes three arguments: currentValue: required. The current element in the array being processed. Index: Optional. The index value of the current element. Arr: Optional. Method is operating on an array. ThisArg: Optional. Used as the value of this (reference object) when the callback function is executed.

Three, the instance,

Print out the contents of the array:

let arr = [4, 9, 16, 25]
    arr.forEach((val, index, arr) => {
        console.log(index)
    })
    var newArr = arr.map((val, index, arr) => {
        return val * 2;
    })
    var roots = arr.map(Math.sqrt);
    console.log(newArr)
    console.log(roots)
Copy the code

7. Map method

 var arr = ['1', '4', '3']
    var newArr = arr.map((value, index, array) => {
        return value
    })
    var newArr1 = arr.map(str => parseInt(str));
    var newArr2 = arr.map(Number)
    console.log(newArr)// '1', '4', '3'
    console.log(newArr1)// 1 4 3
    console.log(newArr2)// 1 4 3
Copy the code

8. Set method

A Set, like a Map, is a Set of keys but does not store values. Because the key cannot be repeated, there is no duplicate key in Set.

var s = new Set([2, 3, 3, '3', 4]); console.log(s); //Set {2, 3,'3', 4,} // add a key. // Duplicate elements are filtered automatically in Set. Dd (5); console.log(s); Set {2, 3, 4,5} // delete a key. //Set{3, "3", 4, 5}// Note that the number 3 and string '3' are different elements. console.log(s);Copy the code