Methods of arrays in ES5

  1. concat( ) : Array merge. The concat() method does not change the existing array. It always returns a new array
  2. join( ) : Array to string.
  3. pop( ) : Delete the last element.
  4. push( ) : Add the array backward.
  5. unshift( ) : The array is added forward.
  6. reverse( ) : The array is reversed.
  7. shift( ) : Delete the first element.
  8. slice( ) : The interception of array elements, returns a new array, the new array is the intercepted element, and it can be a negative value.
  9. sort( ) : sort the array elements;
  10. splice( ) : delete elements and add new elements to the array;
  11. toString( ) : Array to string;
  12. toLocaleString( ) : Convert the array to a local array.
  13. forEach( ) : traverse the array;
  14. map( ) : When there is no return, the traversal of the array. When there is a return, a new array is returned. The elements of the new array are filtered (logically processed) functions.
  15. filter( ) : filter.
  16. every( ) : Returns true when each element in the array is returned true on the callback. (Note: Every is actually similar to filter, except that its function is to determine whether all elements in the array meet the conditions and return a Boolean value).
  17. some( ) : Returns true when there is an element in the array that returns true on the callback. (Note: Every is actually similar to filter, except that its function is to determine whether all elements in the array meet the conditions and return a Boolean value).
  18. reduce( ) : There are 4 parameters in the callback function. prev (previously calculated value), next (previously calculated next value), index, arr. Calculate the array list into a single value.

1. forEach
traverses the array from beginning to end, calling the specified function for each element
. The first parameter: the passed function.
The parameters called by the function: array element, element index, array itself

example:

Insert picture description here
2. Map()
calls each element of the array to the specified function and returns a new array containing the return value; the function passed to map() has a return value, map() returns a new array, and does not modify the called Array; if it is a sparse array, the sparse array is returned in the same way

Example:
Insert picture description here
filter()
returns a subset of the array. The callback function is used to logically determine whether to return. If true is returned, the current element is added to the returned array, and false is not added; the
new array only contains the value of true, and the missing index is not Including, the original array remains unchanged

Example:
Insert picture description here
4.indexOf()lastindexOf() is
used to find the position of the specified element in the array. After finding the first one, it returns its index value. If not, it returns -1; indexOf() searches from the beginning to the end; lastIndexOf() is the opposite To search;
参数1. The value to be searched for the first thing; 2. The second parameter (optional) specifies an index in the array, that is, where to start the search, if omitted, indexOf() searches from the beginning; lastIndexOf() The second parameter of the reverse search can be a negative number, -1 means the last element of the array

Example:
Insert picture description here
reduce()/reduceRight()
uses the specified function to combine the elements of the array to generate a single value
参数1. The function to perform the reduction operation; 2. (Optional) The parameter is the initial value passed to the function to reduce the minimum value from the index Start, deduceRight (reverse)

example:
Insert picture description here

every()/some()
every is true when every callback function of "all" functions returns true, and terminates execution when it encounters false, and returns false;
some function is "purely" with a callback function When it returns true, it returns true if execution is terminated, otherwise it returns false;
calling every on an empty array returns true, some returns false

example:
Insert picture description here

Guess you like

Origin blog.csdn.net/zxlong020/article/details/108477014