JS array traversal and array method

Array traversal

  • The so-called traversal of the array is to take out all the elements in the array
  • Generally use a for loop to traverse the array

Insert picture description here

  • js also provides a method to traverse the array;

forEach( );

Insert picture description here

  • The forEach() method requires a function as a parameter;

  • Functions like this kind of function created by us but not called by us are called callback functions

  • There are several elements in the array. The function will be executed several times. Each time it is executed, the browser will pass in the traversed elements in the form of actual parameters. You can define formal parameters to read these contents.

  • The browser will pass three parameters in the callback function

  • The first parameter is the element currently being traversed

  • The second parameter is the index of the element currently being traversed

  • The third parameter is the array currently being traversed

slice( )

Can be used to extract specified elements from the array

  • This method does not change the original array, but encapsulates the intercepted elements into a new array
  • Parameters:
    1. The index of the start position of the interception, including the start index
    2. The index of the end position of the interception, not including the end index
  • The second parameter can be omitted, and all elements after the start index will be intercepted at this time
  • The index can be a negative value, if a negative value is passed,
    -1 is calculated from the back to the first, the last one

Insert picture description here

splice( )

  • Can be used to delete specified elements in the array
  • Using splice() will affect the original array, delete the specified element from the original array, and return the deleted element as the return value
  • parameter:
  • The first parameter indicates the index of the starting position
  • The second parameter indicates the number of deletions
  • The third and later can pass some new elements, these elements will be inserted in front of the index of the start position

Insert picture description here
Use the splice() method to delete duplicate elements in the array
Insert picture description here

concat( )

concata() can concatenate two or more arrays, separated by commas and return the new array

  • This method will not affect the original array

Insert picture description here
Insert picture description here

Join( )

This method can convert an array into a string

  • This method does not affect the original array, but returns the converted string as the result
  • You can specify a string as a parameter in join( ), and this string will become the connector of the elements in the array

Insert picture description here
Insert picture description here

reverse( )

  • This method is used to reverse the array (from the front to the back, and from the back to the front)
  • This method will directly modify the original array

Insert picture description here
Insert picture description here

Sort( )

  • This method will sort the elements in the array
  • It will also affect the original array, it will be sorted according to Unicode encoding by default
  • Even an array of pure numbers will be sorted according to the Unicode encoding
  • You can specify the sorting rules yourself. You can add a callback function to sort() to specify the sorting rules
    . Two formal parameters need to be defined in the callback function; the
    browser will use the elements in the array as the actual parameters to call the callback function.
    • Browser to determine the order of elements in accordance with the return value of the callback function
      if the return of a value greater than 0, will change position
      if the return value is less than 0, then the position of the element unchanged
      if returns 0, that two equal elements, Do not swap positions

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_48769418/article/details/110308865