Common Array object methods

  • concat()
    • Joins two or more arrays
    • Do not change the original array
    • Returns a copy of the joined array
      let a = [1.2.3]
      let b = [3.4.5]
      let c = a.concat(b)
      console.log(a) / / [1, 2, 3]
      console.log(b) / / / three, four, five
      console.log(c) / /,2,3,3,4,5 [1]
    Copy the code

  • join()
    • Delimits the data in the array using the specified delimiter and returns a string
    • Do not change the original array
    • Return string
          let a = [1.2.3]
          let b = a.join('. ')
          console.log(a) // 1,2,3
          console.log(b) / / 1.2.3
      Copy the code

  • slice()
    • Returns the element at the selected position from an existing array summary
    • Do not change the original array
    • Returns a new array
      let a = [1.2.3]
      let b = a.slice(0.2)
      console.log(b) / / [1, 2]
      Copy the code

  • toString()
    • Convert an array to a string
    • Do not change the original array
    • Returns the string form of an array
      let a = [1.2.3]
      let b = a.toString(a)
      console.log(b) / / 1, 2, 3
      Copy the code

  • pop()
    • Deletes the last element of the array. If the array is empty, the array is not changed and undefined is returned

    • Change the original array

    • Returns the deleted array

      let a = [1.2.3]
      let aa = []
      let b = a.pop()
      let bb = aa.pop()
      console.log(b) / / 3
      console.log(bb) // undefined
      Copy the code

  • push()
    • Adds one or more elements to the end of the array
    • Change the original array
    • Returns the length of the new array (no return value, no chain structure)
      let a = [1.2.3]
      let b = a.push(4.5.6)
      console.log(a) // [1, 2, 3, 4, 5, 6] changes the original array
      console.log(b) / / 6
      Copy the code

  • reverse()
    • Reverses the order of the elements in an array
    • Change the original array
    • Return the array
      let a = [1.2.3.4]
      let b = a.reverse()   
      console.log('a',a) //a [4, 3, 2, 1] changes the original array
      console.log('b',b) //b [ 4, 3, 2, 1 ]
      Copy the code

  • shift()
    • Delete the first element of the array, if the array is empty, do not operate, return undefined
    • Change the original array
    • Returns the value of the first element removed
      let a = [1.2.3.4]
      let b = a.shift()
      console.log(a)// [2, 3, 4] √
      console.log(b)/ / 1
      Copy the code

  • sort()
    • Sort array elements (according to the ASCII table)
    • Change the original array
    • Return the array
      let a = [3.2.1]
      let b = a.sort()
      console.log(a) // [1, 2, 3] changes the original array
      console.log(b) // [1, 2, 3]
      Copy the code

  • splice()
    • Adds/removes items from an array
    • Change the original array
    • Returns the deleted element
      let a = [1.2.3.4]
      let b = a.splice(1)
      console.log(a) // [1] Changes the original array
      console.log(b) // [2, 3, 4] returns the deleted element
      Copy the code

  • unshift()
    • Adds one or more elements to the beginning of an array
    • Change the original array
    • Returns the length of the new array
      let a = [1.2.3.4]
      let b = a.unshift(-1.0)
      console.log(a)  // [-1, 0, 1, 2, 3, 4]
      console.log(b) // The length returned by 6
      Copy the code