1, the loop

1. For loop

We use for when we know how many times we’re going to execute it

 forLoop variable (expression1); Loop conditions (expressions2); Variable changes (expressions3)){loop body; }Copy the code

2. The while loop

When we do a conditional loop, we usually use while, which is the number of times we do an indeterminate loop, and we terminate the loop when a condition is reached

 while(true){circulatory body; }Copy the code

3. Switch loop

If you have a lot of deterministic conditions, you can use this instead of the else if all the time

 switch(variable/expression){case1Operation:1;
     case2Operation:2;
     case3Operation:3;
     default: Default operation; }Copy the code

2, array

1. Basic concepts

  1. What is an array

    A collection of data (of different types) held in memory

  2. When to use arrays

    You can use arrays whenever you want to store multiple related data

2. Create an array

  1. Direct quantity mode

     const arr = [1.2.3]
    Copy the code
  2. The constructor

     const arr = new Array(1.2.3)
     ​
     / / special
     const arr = new Array(8) // This is not [8], which means that an array of length 8 is created,
    Copy the code
  3. Access to the elements

    Arr (subscript)

    Return undefined if not read

  4. Sometimes limit

    1. There is no limit to the number of elements
    2. There is no restriction on the type of the element
    3. Don’t limit subscripts out of bounds – not a good thing

3. Array APIS

None of the following changes the original array

  1. Arr to string — join

     const arr = [1.2.3.4.5]
     const newArray = arr.join() // You can customize the concatenation, default is a comma link
     const newArray1 = arr.join("")
     const newArray2 = arr.join("-")
     console.log(newArray);  / / 1, 2, 3, 4, 5
     console.log(newArray1);  / / 12345
     console.log(newArray2);  / / the 1-2-3-4-5
    Copy the code
  2. Concatenated array — concat

     const arr = [1.2.3.4.5]
     const newArray = arr.concat([6.7.8])
     console.log(newArray); // [1, 2, 3, 4, 5, 6, 7, 8]
    Copy the code
  3. Slice (start, end)

     cosnconst arr = [1.2.3.4.5]
     const newArray = arr.slice(0.2)
     const newArray1 = arr.slice(-3, -1)
     console.log(newArray); // [1, 2
     console.log(newArray1); // [3, 4] -- open and close -- negative numbers also work
    Copy the code

All of the following will change the original array

  1. Delete – Add – Replace arr.splice()

    // delete arr. Splice (start, delete several)
    const arr = [1.2.3.4.5]
    const newArray = arr.splice(1.2)
    console.log(arr); / / [1, 4, 5]
    console.log(newArray); / / [2, 3]
    
    
    
    // Add arr.splice(start, 0, added) -- delete 0
    const arr = [1.2.3.4.5]
    const newArray = arr.splice(1.0.10.11.12) // add 10,11,12 after the first element
    console.log(arr); // [1, 10, 11, 12, 2, 3, 4, 5]
    console.log(newArray); / / []
    
    
    
    // Replace arr.splice(start, delete, add)
    const arr = [1.2.3.4.5]
    const newArray = arr.splice(1.3.10.11.12) // Delete 3, then add 3,
    console.log(arr); // [1, 10, 11, 12, 5]
    console.log(newArray); / / [2, 3, 4]
    Copy the code

Pay attention to

When the splice method involves deleting, it returns a new array of the deleted elements

  1. Reverse array arr.reverse()
const arr = [1.2.3.4.5]
arr.reverse()
console.log(arr); //[5, 4, 3, 2, 1]
Copy the code

\