Array method collation

Array toString(toString())
    // Convert an array to a comma-separated string of array values
    var color = ["red"."blue"."pink"]
    console.log(color.toString());   //red,blue.pink
Copy the code
All elements of an array are combined into a single string (join())
    // Similar to toString(), but you can specify your own separator
     var color = ["red"."blue"."pink"]
     console.log(color.join("-"));   //red-blue-pink
     
     // The separators can be *, &, and %, but they should be quoted as ""
Copy the code
Add or remove elements from end of array (push() and pop())
    // The push() method adds a new element to the end of the array and returns the modified length
    var arr = [1.2]
    console.log(arr.push(3));   //3,(return array length)
    console.log(arr);   / / [1, 2, 3]
    arr.push("red"."pink");   // The string should be quoted
    console.log(arr);   / / [1, 2, 3, "red", "pink"]
    arr.push(2+2.2+3)   // Can also perform simple operations
    console.log(arr);   / / / / [1, 2, 3, "red", "pink", 4, 5)
    
    The pop() method removes the last element from the array and returns the deleted value, only one at a time
    console.log(arr.pop());   / / 5
    console.log(arr.pop());   / / 4
    console.log(arr);   / / [1, 2, 3, "red", "pink"]
Copy the code
Add or remove elements at the beginning of an array (shift() and unshift())
    // The unshift() method adds the argument to the beginning of the array and returns the modified array length
     var arr = [1.2]
    console.log(arr.unshift(0));   //3,(return array length)
    console.log(arr);   / / [0]
    / /... Similar to the push() method above
    
    // Shift () deletes the first element of the array and returns the value of the deleted element, or undefined if the array is empty
     var arr = [1.2]
    console.log(arr.shift());   //2,(return array length)
    console.log(arr);   / / [1, 2]
Copy the code
Change the element
  1. Change elements by index
    [0] is the first array element, [1] is the second array element, [2] is the third array element...
    var color = ["red"."black"."blue"]
    color[0] ="orange"
    console.log(color);   //["orange","black","blue"]
Copy the code
  1. Add elements by length
   //length indicates the length of the array, and the index number is always 1 less than length
    var color = ["red"."black"."blue"]
    color[color.length]="orange"
    console.log(color);   //["red","black","blue","orange"]
Copy the code
Splice array (splice())
    / / splice () method
    // The first argument (2) defines where the new element should be added. The index position is the starting position, including the value of the current index
    The second argument (0) defines how many elements should be deleted
    // The remaining parameters (" Skyblue ","orange") define the element to be added, which can be multiple
    var color = ["red"."black"."blue"]
    color.splice(2.0."skyblue"."orange")
    console.log(color);   //["red","black","skyblue","orange","blue"]
    
    // The splice() method can also be used to delete elements
    var color = ["red"."black"."blue"]
    color.splice(1.2);
    console.log(color);   //["red"]
Copy the code
Merge arrays (concat())
    // The concat() method does not change the original array; it always returns a new array
    // You can take any number of arrays as arguments, separated by commas
    var arr1 = [1.2.3];
    var arr2 = [4.5.6];
    var arr3 = [7.8.9];
    var myArr = arr1.concat(arr2,arr3);
    console.log(myArr);   / /,2,3,4,5,6,7,8,9 [1]
Copy the code
Tailoring array
    The slice() method creates a new array that does not remove any elements from the source array
    The slice() method takes two arguments, such as (1,3)
    // The fragment is truncated from index number 1 until the end parameter (excluding the end parameter data)
    // If the argument is negative, add the value of the array length to the negative value
    var color = ["red"."black"."blue"]
    var cut = color.slice(1.3);
    console.log(cut);   //["black","blue"]
Copy the code

Array sort(sort())

    The sort() method sorts arrays alphabetically
    var arr = ["a"."d"."b"."c"."e"]
    console.log(arr.sort());   //["a","b","c","d","e"]
Copy the code
Reverse (reverse())
The reverse() method reverses the elements in an array and can be used to sort the array in descending ordervar arr = ["a"."b"."c"]
    arr.reverse();   //["c","b","a"]
Copy the code

Digital sorting

    var arr = [1.66.28.99.100]
    arr.sort(function(a,b){return a-b});   / /,28,66,99,100 [1]
    //sort will pass in two data points and reverse if the result is negative
Copy the code
Math methods
useMath.max.apply to find the highest value in the arrayvar var arr = [3.56.22.100]
    Math.max.apply(null,arr);   / / 100
    
    // Use math.min. apply to find the lowest value in the array
    var var arr = [3.56.22.100]
    Math.min.apply(null,arr);   / / 3
Copy the code

JavaScript array iteration

The forEach () method

Call a function once for each array element (callback function)

    ForEach's callback takes three arguments and does not return a value
    // The first argument is the current (required) item of the array
    // The second argument is the index number of the current element
    // The third argument is the array object to which the current element belongs
    var arr = [1.2.3]
    arr.forEach(item= >{
        item*2
    })
    console.log(arr)   / / [1, 2, 3]It doesn't change the array, it just iterates over each element and executes a callbackCopy the code
The map () method

The map() method creates a new array by executing a function on each array element

    The map() method has the same arguments as the forEach() method, but map() returns an array of the results of each function call
    var arr = [1.2.3]
    arr.map(function(item,index,array){
      return item*2
    });   // the result is [2,4,6]
    // It can also be written as an arrow function. When the callback function uses only the item argument, other arguments can be omitted
    arr.map(item= > item*2);   / / minus [2]
Copy the code
The filter () method

The filter() method creates a new array containing the array elements that pass the test

The parameter is the same as forEach(), but the filter() method returns the elements that meet the criteria to create a new arrayvar numbers = [5.16.20.25.50]
    numbers.filter(function(item){retrun item>18});
    //[20,25,50] returns a new array of elements greater than 18
Copy the code
Reduce () method
Grammar: arr. Reduce (callback, [the initialValue])// callback (execute a function for each value in the array, containing four arguments)

// 1, previousValue (the value returned from the last call to the callback, or the initialValue provided)
// 2, currentValue (array element currently being processed)
// 3, index (current element index in array)
// 4, array (call reduce array)

// initialValue (as the first argument to the first callback call.)

    var arr = [10.20.20.30.30.55]
    arr.reduce((pre,cur) = >{
       if(! pre.includes(cur)){ pre.push(cur); }return pre;
    },[]);   //[10,20,30,55], the initial value is an empty array, which can be de-duplicated
Copy the code
Every () method

The every() method determines whether each item in the array satisfies the condition and returns true only if all items satisfy the condition

    var arr = [1.2.3.4.5];
    var arr2 = arr.every(function(x){return x<10});
    console.log(arr2);   //true
    var arr3 = arr.every(function(x){return x<3});
    console.log(arr3);   //false
Copy the code
Some () method

The some() method determines whether there are any items in the array that satisfy the condition, and returns true whenever one item satisfies the condition

    var arr = [1.2.3.4.5];
    var arr2 = arr.some(function(x){return x<3});
    console.log(arr2);   //true
    var arr3 = arr.some(function(x){return x<1});
    console.log(arr3);   //false
Copy the code
IndexOf () method

The indexOf() method searches for an element value in an array and returns its position (index number)

    //indexOf() takes two arguments
    //item, required, to detect the searched item
    //start, optional, where to start the search, negative values start at the end, and search to the endIf no item is found, return -1; If multiple occurrences occur, return the index of the first occurrencevar arr = [1.2.3]
    console.log(arr.indexOf(3));   //2, index position
    console.log(arr.indexOf(10));   //-1
Copy the code
The lastindexOf () method

Array.lastindexof () is similar to array.indexof (), but the search begins at the end of the Array

The find () method

The find() method returns the value of the first array element that passes the test function

    var arr = [3.2.6.20.19.69]
    var first = arr.find(function(item){return item>10});
    console.log(first);   / / 20
Copy the code
FindIndex () method

The findIndex() method returns the index number of the first element

    var arr = [3.2.6.20.19.69]
    var first = arr.findIndex(function(item){return item>10});
    console.log(first);   / / 3
Copy the code