Reprint of this article is prohibited without permission

An array of

Can change the original array

  • Array.push() adds one or more elements to the end of the Array and returns the new Array length. The original array changes.

        / / principle
    Array.prototype.push = function () {
        for (let i = 0; i < arguments.length; i++) {
            this[this.length] = arguments[i];
        }
        return this.length
    }
    / / such as
    let arr = [1.2.3.4]
    arr.push(5.6.7)
    arr = [1.2.3.4.5.6.7]
    Copy the code
  • Array.pop(), which removes and returns the last element of the Array, or undefined if the Array is empty. The original array changes.

    / / such as
    arr = [1.2.3.4.5.6.7]
    arr.pop()   // del = 7
    // arr = [1, 2, 3, 4, 5, 6]
    Copy the code
  • Array.unshift() adds one or more elements to the beginning of the Array and returns the new Array length. The original array changes.

    let arr = [1.2.3.4.5.6.7]
    let res = arr.unshift(0)   // res = 8 returns the array length
    // arr = [0, 1, 2, 3, 4, 5, 6, 7]
    Copy the code
  • Array.shift(), removes the first item of the Array and returns the value of the first element. If the array is empty, undefined is returned. The original array changes.

    let arr = [1.2.3.4.5.6.7]
    let res = arr.shift()   // res = 1 returns the first element
    //arr = [2, 3, 4, 5, 6, 7]
    Copy the code
  • Array.reverse(), which reverses the Array and changes the original Array.

    let arr = [1.2.3.4.5.6.7]
    	arr.reverse()   // arr = [7, 6, 5, 4, 3, 2, 1]
    Copy the code
  • Array.splice (from the number of bits to be cut, add new data at the cut), when the cut length is 0, the element is not deleted. The original array changes.

    let arr = [1.2.3.4.5.6.7]
    	arr.splice(2.1)    // Delete a length from the second digit
    	arr.splice(2.1.4)    // Take a length from the second
    	//arr = [1, 2, 4, 4, 5, 6, 7]
    	arr.splice(2.0.4)  // When the intercept length is 0
    	// arr = [1, 2, 4, 3, 4, 5, 6, 7]
    Copy the code
  • Array.sort(), which sorts Array elements. Sort by string Unicode code. The original array changes. Arr. Sort (1. Must write two parameters 2. When the return value is positive, the next number comes first; Fixed when return value is 0)

    / / such as:
    let arr = [1.3.5.4.10]
    	arr.sort(function (a, b) {
                 if(a > b) {       // a < b is in descending order
            return 1
        }else {
            return -1}})// Simplify
    arr.sort(function (a, b)) {
             return a - b;     // Change to b-a in descending order
             }
    // Give an ordered array out of order
    let arr = [1.2.3.4.5.6]
    	arr.sort(function() {
            return Math.random() - 0.5
        })
    Copy the code
You cannot change the original array
  • Array.concat(), merges two or more arrays to generate a new Array. The original array remains the same.

    let arr = ["x"."y"."z"]  / / the same
    let arr1 = [1.2.3]   / / the same
    let arr2 = [4.5.6]  / / the same
    let rel = arr.concat(arr1,arr2)
    //rel = ["x","y","z", 1, 2, 3, 4, 5, 6]
    
    // 6. (Optional)
    // Customize the concat method
    function concat (arr1, arr2) {
      for (i = 0; i < arr2.length; i++) {
          arr1.push(arr2[i])
      }
      return arr1
    }
    concat([1.2.3], [4.5.6])
    Copy the code
  • Array.join(), which joins each item of the Array to a string with the specified character. The default link string is “, “, comma.

    let arr = [1.2.3.4.5]
          arr.join()  //"1, 2, 3, 4, 5"
          arr.join("-")  The 1-2-3-4-5 "/ /"
    Copy the code
  • Array.slice(start,end), starting from start and ending before end, excluding end. If end is not given, start to end of array. Start can be given a negative value, -1 to indicate the last bit of data, and so on. Null values not written represent the entire interception.

    let arr = [1.2.3.4.5]
    let arr1 = arr.slice(2.4)  // arr1 = [3,4] slice return value
    let arr2 = arr.slice(1)    // arr2 = [2, 3, 4, 5]
    let arr3 = arr.slice(-2)   // arr3 = [4, 5]
    Copy the code
  • Array.indexof () returns the first index in the Array where a given element can be found, or -1 if none exists.

    ['a'.'b'.'c'.'a'.'b'].indexOf('b');   / / 1
    ['a'.'b'.'c'.'a'].indexOf('d');   // -1
    Copy the code
  • Array.lastindexof () returns the last index in the Array or -1 if it does not exist

    ['a'.'b'.'c'.'a'].lastIndexOf('a');   / / 3
    ['a'.'b'.'c'.'a'].lastIndexOf('d');   // -1
    Copy the code
  • Array.includes() checks whether an Array contains an element and returns a Boolean value

    let site = ['runoob'.'google'.'taobao'];
    site.include('runoob')   / / return true
    Copy the code

ES5/ES6 new array methods

  • IsArray () checks whether it is an array and returns a Boolean value to compensate for typeof detection

    let arr = [1.2.3]
    console.log(typeof arr) // "Object"
    Array.isArray(arr) // true
    Copy the code
  • The forEach() method performs the provided function once on each element of the array. Function equivalent to for loop. Application scenario: Bind event handlers for some of the same elements!

    var arr = ["Zhang fei"."Guan yu"."Zhaoyun"."D"];
    // The first argument: element, each element of the array
    // The second argument: index, the index of the array
    // The third argument: array, the array being traversed
    arr.forEach(function(element, index, array){
      console.log(element, index, array);
    });
    Copy the code
  • The map() method iterates through the array, returning a new array. The new array is a mapping of the original array. This method does not change the value of the original array. Each element of the new array is the value returned each time the return is iterated

    let arr = [1.2.3.4.5]
    let newArr = arr.map(item= > return item * 2)
    console.log(newArr)  / /,4,6,8,10 [2]
    Copy the code
  • Filter () returns a new array. If the return value is true, it stays; If false is returned, filter it out

    let arr = [1000.5000.20000.3000.10000.800.1500];
    var newArray = arr.filter(function(element, index, array){
      if(element > 5000) {
        return false;
      }else {
        return true; }});console.log(newArray);//[1000, 5000, 3000, 800, 1500]
    Copy the code
  • Some () iterates through a list of numbers, returning true as long as one of the conditions is met and false otherwise

    let arr = [2.4.6.8.10.21];
    let result = arr.some(item= > {
      if(element %2= =1) {return true;
      }else {
        return false; }})console.log(result)   // true
    Copy the code
  • Every () iterates through the array and is true only if each element of the array returns true if there is a false

    let arr = [1.2.3.4.6.8.4]
    const result = arr.every(item= > item % 2= = =0)
    console.log(result)  // false
    Copy the code
  • The reduce() traversal number set takes two arguments. The first argument is a function, and the second argument is the initial value of the function iteration. This method is commonly used for array summation/product, counting the number of occurrences of elements, and de-duplication

    let arr = [1.2.3.4]
    let result = arr.reduce((prev,el,index,arr) = > {
        // prev The value returned by the last function call
        // el Current element
        // index Indicates the index of the current element
        // arr the array to be traversed
        return prev + el
    },5)
    console.log(result) //  15
    Copy the code
  • Find () This method receives a callback function that returns the value of the first element that satisfies the function’s execution

    let arr = [2.3.4.6.7.8]
    let result = arr.find(item= > item > 4)
    console.log(result) / / 6
    Copy the code
  • FindIndex () This method receives a callback function that returns the index of the first element that the function executes, or -1 if it does not

    let arr = [2.3.4.6.7.8]
    let result = arr.findIndex(item= > item > 4)
    console.log(result) / / 3
    Copy the code

string

  • Slice (start,end) starts from start to end without getting end

    let str = 'string'
    console.log(str.slice(2.4))  // ri
    Copy the code
  • Split () converts a string to an array

      //split: split a string into an array (very common)
      // Just the opposite of an array join.
    
      / / var STR = 'zhaoyun | d | guan yu';
    
      // The first case is rarely used without passing arguments
      // var arr = str.split();
      // console.log(arr); / / / "zhaoyun | d | guan yu"]
    
      // The second case passes a delimiter
      // var arr = str.split('|');
      // console.log(arr); // [" Zhao Yun ", "Ma Chao "," Guan Yu "]
    
      // The third case passes a null value
      // var arr = str.split('');
      // console.log(arr); / / / "zhao", "cloud", "|", "horse", "super", "|", "close", "feather"]
      ` ``
    
    Copy the code
  • Substring (start, end) The string is truncated from start to end without end

  • Substr (start, end) String truncation Truncates the end bit starting from start

    let str = 'string'
    console.log(str.substr(2.4))  // ring
    Copy the code
  • The indexOf() traversal string takes an argument to determine whether it contains the index if it does, and returns -1 if it does not

    let str = 'string'
    str.indexOf('t') / / 1
    Copy the code
  • ToUpperCase () : converts all toUpperCase; ToLowerCase () : All lowercase letters

  • Replace (searchValue, replaceValue) Replaces the string. If there are duplicate characters, only the first one is replaced

    // Parameter: searchValue: the value to be replaced replaceValue: the value to be replaced
    let str = 'sttring'
    str.replace('t'.'z') // szring
    Copy the code