Home >Web Front-end >JS Tutorial >Summary and analysis of JavaScript array methods_javascript skills
Since I have been coding on freecodecamp recently and used a lot of JavaScript array methods, I made a compilation of JavaScript tutorials. The specific content is as follows:
1. Ordinary method
1. join() joins array elements together and returns them in string form
Parameter: Optional, specifies the separator between elements. If there is no parameter, it defaults to comma
Return value: string
Impact on original array: None
2. reverse() changes the order of the elements of the array into reverse order and returns
Parameters: None
Return value: array
Impact on the original array: The original array is modified into an array arranged in reverse order
3. sort() sorts the array elements and returns
Parameters: optional, sorting method function, without parameters, the default is to sort in dictionary order
Return value: sorted array
Impact on the original array: The original array is modified into a sorted array
4. concat() connects several arrays
Parameters: several, which can be arrays or elements,
Return value: New array after concatenation
Impact on original array: None
5. slice() cuts out several elements from the array, forms a new array and returns
Parameters: two numbers, the second of which is optional. The first parameter indicates the index value of the first element to be intercepted (this element is included when intercepting). If the first parameter is a negative number, it means that the intercepted element starts from Counting from the end of the element (for example: -1 means the last element); the meaning of the second parameter is the index value of the element to stop intercepting (this character is not included when intercepting), the negative value is the same as the first parameter
Return value: intercepted new array
Impact on original array: None
6. splice() replaces, deletes or inserts elements from the array and returns a new array
Parameters: Several parameters, of which the first parameter is required and the others are optional. The first parameter is the first index value of the operation. If there is no second parameter at this time, the first parameter (including the first parameter) will be deleted. All elements after the index value of each parameter), when the second parameter is included, the second parameter deletes the number of elements and returns a new array composed of these elements; when the second parameter is 0, the following parameters will Insert the original array as a new element and return an empty array; when the second parameter is not 0 and contains other parameters, the replacement operation is performed and the new array composed of the original elements before replacement is returned
Return value: new array composed of deleted elements
Impact on the original array: Replacement, deletion, insertion, etc. operations will be performed on the original array
7. push() adds elements to the end of the array and returns the array length
Parameters: several, elements added to the end of the array
Return value: length of the array after adding elements
Impact on the original array: elements
8. pop() deletes an element from the end of the array
Parameters: None
Return value: deleted element
Impact on the original array: One element is deleted from the end of the original array
9. unshift() adds elements to the head of the array and returns the array length
Parameters: several, elements added to the head of the array
Return value: length of the array after adding elements
Impact on the original array: elements
10. shift() deletes an element from the head of the array
Parameters: None
Return value: deleted element
Impact on the original array: One element is deleted from the head of the original array
11. toString() converts the array into a string, with commas separating each element
Parameters: None
Return value: The formed string (in a two-dimensional array, only the elements of the two-dimensional array are connected)
Impact on original array: None
12. toLocaleString() is the localized version of toString() method
-------------------------------------------------- ----------------------------------
2. Iterator method
1. foreach() calls the method on each element of the array
Parameters: a function
Return value: None
Impact on original array: None
2. every() accepts a function whose return value is Boolean type. If the function returns true for all elements in the array, it will return true, otherwise it will return false
Parameters: a function whose return value is Boolean
Return value: true or false
Impact on original array: None
3. some() accepts a function with a return value of Boolean type. As long as there are elements in the array, the function returns true, otherwise it returns false
Parameters: a function whose return value is Boolean
Return value: true or false
Impact on original array: None
4. map() accepts a function as a parameter and returns a new array. The elements of the new array are the results of using the function on the original array elements
Parameters: a function
Return value: an array composed of each element using the result value of the function
Impact on original array: None
5、filter()接收一個傳回值為布林值的函數作為參數,對所有元素應該該函數,並傳回傳回值為true的元素組成的新陣列
參數:一個函數
傳回值:每個元素使用函數為true的元素組成的陣列
對原數組的影響:無
-------------------------------------------------- ------------------------------
三、歸併方法
1、reduce()接受一個函數作為參數,並傳回一個值。從一個累加值開始, 不斷對累加值和陣列中的後續元素呼叫函數。
參數:一個函數
傳回值:最後的累加值
對原數組的影響:無
2、reduceRight()方法
說明:和reduce一樣,只是執行順序是從右到左
以上這篇JavaScript陣列方法總結分析就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。