JS Red Book (1) Array of Notes


Title: JS Red Book (1) Array
Date: 2020-05-18 11:06
categories: JS Red Book
tags:

  • Array

Array

Stack method

  • push()

    Accept any number of parameters, add them one by one to the end of the array, and return the length of the modified array.

  • pop()

    Remove the last item from the end of the array, reduce the length value of the array, and return the removed item.

Queue method

  • shift()

    Remove the first item in the array and return the item, and reduce the length of the array by one.

  • unshift()

    Add any item to the front of the array and return the length of the array.

Reordering method

  • reverse()

    Reverse array sort

  • sort()

    By default, the sort() method sorts array items in ascending order (the smallest is at the top, and the largest value is at the bottom). In order to achieve sorting, the sort() method calls the toString() transformation method of each array item, and then compares the resulting strings.

Operation method

  • concat()

    This method will first create a copy of the current array, then add the received parameters to the end of the copy, and finally return the newly created array. Without passing parameters to the concat() method, he just copies the current array and returns the copy. If one or more arrays are passed to the concat() method, the release will add each item of these arrays to the result array.

  • slice()

    The slice() method can accept one or two parameters, that is, the starting and ending positions of the item to be returned. In the case of only one parameter, the parameter is the starting position, and all items from the starting position to the end of the current array are returned. The slice() method does not affect the original array.

  • splice()

    • Delete : You can delete any item in the array, you only need to specify two parameters: the location to delete and the number of items to delete. For example, splice(0,2) will delete the first two items in the array.
    • Insert : You can insert any number of items into the specified position, you only need to provide three parameters: the starting position, 0 (the number of items to be deleted) and the item to be inserted (there can be more than one). For example: splice(2,0,"dog","cat")
    • Replace : You can insert any number of items into the specified position and delete any number of items at the same time. The number of items inserted does not have to be the same as the number of items deleted. For example: splice(2,1,"dog","cat")

Iterative method

  • every(): Run a given function for each item in the array, and if the function returns true for each item, it returns true.
  • filter(): Run a given function on each item in the array, and return an array of items whose function returns true.
  • forEach(): Run a given function for each item in the array, and the function has no return value.
  • map(): Run a given function on each item in the array and return an array composed of the results of each function call.
  • some(): Run a given function on each item in the array, and return true if any item of the function returns true.

Merge method

  • reduce(): Start from the first item of the array and traverse one by one to the end.
  • reduceRight(): Starting from the last item of the array, traverse forward to the first item.

The functions passed to reduce() and reduceRight() accept 4 parameters: the previous value, the current value, the index of the item, and the array object.

Guess you like

Origin blog.csdn.net/weixin_44555878/article/details/106588333