directory

  1. For Operation Description
  2. For traverses the array
  3. For traverses the string
  4. For cannot traverse an object
  5. Break and continue

0, for operation description

for(var i=0; i<100; i++){/* var i=0; Is to define the initial variable. i<100; It's a cyclic condition. i++; Is the variable */
    console.log(i)/* Loop statement */
}
Copy the code
forDefine the initial variable ①; Cyclic conditions ②; ④) {loop statement; (3)}Copy the code

Execution order: ①②③④ ②③④②③ ③④…… Until I don’t

① Define initial variables: execute semicolons in parentheses only once before entering the loop

② Cycle conditions: each time into the cycle will execute and judge **

④ Variable change value: each time the loop is completed, ③ the content executed after the loop statement

For iterates through an array

1) data

let data = new Array(50).fill().map((item,index) = > index);
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
Copy the code

2) Iterate over the data

Iterating by index, values are evaluated by index, such as data[0]

let data = new Array(10).fill().map((item,index) = > index+10);
data[0];//10 can be retrieved by numeric index
console.time('test');
for(let i=0; i<data.length; i++){
    console.log('i:',i, 'data[i]:', data[i]);
}
console.timeEnd('test');// The average time is about '3ms'
Copy the code

For traverses the string

The data in this data type (string) can be retrieved by numeric indexing, so for can be traversed

let data = 'abc';
data[0];//'a' can fetch data from this data type by index, so it can be traversed by for.
for(let i=0; i< data.length; i++){
    console.log(data[i]); // a b c
}
Copy the code

Third, the forUnable to traverse objects

let data = {a:1.b:2};

data[0];// undefined can't get data from this data type by 'numeric index', then 'for' cannot be traversed.
data['a']; // The 1 object can only fetch data by key
data.length;// undefined
Object.keys(data);// ['a', 'b']
for(let i=0; i< data.length; i++){
    console.log(i, data[i]);
}
// undefined
Copy the code

1) The traversal length above cannot be obtained, so modify it as follows

let data = {a:1.b:2};
Object.keys(data);// ['a', 'b']
for(let i=0; i< Object.keys(data).length; i++){
    console.log(i, data[i]);
}
// 0 undefined
// 1 undefined
Copy the code

You can iterate, but you can’t get the value.

2) For can iterate over the specified object type

For traverses only objects whose key is a numeric index

let data1 = {'0':'a'.'1': 'b'};

data1[0];// 'a' fetches data from this data type by 'numeric index', then 'for' can traverse 'this object type'.
data1.length;// undefined
Object.keys(data1);/ / / '0', '1'
for(let i=0; i< Object.keys(data1).length; i++){
    console.log(i, data1[i]);
}
// 0 'a'
// 1 'b'
Copy the code

Break and continue

let arr11 = [1.2.3.4.5];
let j =0;
let length = arr11.length;
for(; j < length; j++){
    console.log('arr[j]:', arr11[j]);
}
Copy the code

1) break

Break is to jump out of the loop

let arr11 = [1.2.3.4.5];
let j =0;
let length = arr11.length;
for(; j < length; j++){
    if(j===3) {break; }console.log('arr[j]:', arr11[j]);
}
Copy the code

1) continue

Contine is breaking out of a single loop of j===3, the whole loop will continue!

let arr11 = [1.2.3.4.5];
let j =0;
let length = arr11.length;
for(; j < length; j++){
    if(j===3) {continue; }console.log(Arr [J]: arR, arr11[j]);
}
Copy the code

conclusion

  1. As long as through theDigital indexcanThe data type to fetch (array or string), you can passforTraverse. Such asstring.An array of
  2. forOnly can traverseKey is a numeric indextheobject