Since I have been using many JavaScript array methods recently, I have compiled a tutorial on JavaScript, and the specific content is as follows:
1. Ordinary method
1. Join() joins array elements together and returns as a string
Parameters: optional, specify the separator between elements, if there is no parameter, the default is a comma
Return value: string
Effect on the original array: None
2. reverse() returns the order of elements of the array into reverse order.
Parameters: None
Return value: array
Effect on the original array: The original array is modified to an array after being arranged in reverse order
3. sort() sorts the array elements and returns
Parameters: optional, sorting method function, if there are no parameters, the default is sorted in dictionary order.
Return value: sorted array
Effect on the original array: The original array is modified to the sorted array
4. Concat() connects several arrays
Parameters: several, can be arrays or elements.
Return value: The new array after connection
Effect on the original array: None
5. slice() cuts off several elements from the array, forms a new array and returns
Parameters: Two numbers, the second one is optional, the first parameter represents the index value of the first element that starts to be intercepted (including this element when intercepted). If the first parameter is a negative number, it means that the intercepted element starts from the end of the element (such as -1 represents the last element); the meaning of the second parameter is the index value of the element that stops to be intercepted (not including this character when intercepted), and the negative value is the same as the first parameter.
Return value: The intercepted new array
Effect on the original array: None
6. splice() replaces, deletes or inserts elements from the array and returns a new array
Parameters: Several parameters, the first parameter is required, the others are optional, the first parameter is the first index value of the operation. When there is no second parameter, all elements after the index value of the first parameter (including the first parameter) will be deleted. When the second parameter contains the second parameter, the second parameter deletes the number of elements and returns a new array composed of these elements; when the second parameter is 0, the subsequent parameters will be inserted into the original array as a new element and return an empty array; when the second parameter is not 0 and contains other parameters, a replacement operation will be performed and the new array composed of the original elements before the replacement is returned.
Return value: Delete a new array of elements
Impact on the original array: Replace, delete, insert and other operations on the original array
7. Push() adds elements to the tail of the array and returns the length of the array
Parameters: Several elements added to the end of the array
Return value: length of array after adding elements
Effect on the original array: elements are added to the tail of the original array
8. pop() deletes an element from the end of the array
Parameters: None
Return value: deleted element
Effect on the original array: An element is deleted at the tail of the original array
9. Unshift() adds elements to the head of the array and returns the length of the array
Parameters: Several elements added to the header of the array
Return value: length of array after adding elements
Effect on the original array: element added to the original number header
10. shift() deletes an element from the head of the array
Parameters: None
Return value: deleted element
Effect on the original array: an element is deleted from the original array header
11. toString() converts array into strings, separated by commas between each element.
Parameters: None
Return value: The formed string (the two-dimensional array is also only the elements that connect the two-dimensional array)
Effect on the original array: None
12. toLocaleString() is the localized version of the toString() method
--------------------------------------------------------------------------------
2. Iterator method
1. Foreach() calls method for each element of the array
Parameter: a function
Return value: None
Effect on the original array: None
2. Every() accepts a function with a Boolean return value. All elements in the array return true, then true, otherwise return false
Parameter: A function with a Boolean return value
Return value: true or false
Effect on the original array: None
3. Some() accepts a function with a Boolean return value. As long as there are elements in the array, the function returns true, otherwise it returns false
Parameter: A function with a Boolean return value
Return value: true or false
Effect on the original array: None
4. map() accepts a function as a parameter and returns a new array. The elements of the new array are the result of the original array element using the function.
Parameter: a function
Return value: an array of results of each element using the function
Effect on the original array: None
5. filter() receives a function with a Boolean value as a parameter, should use the function for all elements, and returns a new array of elements with a return value of true.
Parameter: a function
Return value: Each element uses an array of elements with the function true
Effect on the original array: None
--------------------------------------------------------------------------------
3. Combination method
1. reduce() accepts a function as a parameter and returns a value. Starting with an accumulated value, the function is constantly called on the accumulated value and subsequent elements in the array.
Parameter: a function
Return value: The last accumulated value
Effect on the original array: None
2. ReduceRight() method
Note: Like reduce, the execution order is from right to left
The above summary and analysis of JavaScript array methods is all the content I share with you. I hope you can give you a reference and I hope you can support me more.