Home >Web Front-end >JS Tutorial >JS array (Array) processing function sorting_basic knowledge
1. concat() connects two or more arrays
This method does not modify the existing array, but simply returns a copy of the concatenated array.
For example:
Output result:
1,2,3,4,5,11,22,33
2. join()
Put all elements of the array into a string. Elements are separated by the specified delimiter.
For example:
list results:
'
3. pop() deletes and returns the last element of the array
The pop() method will remove the last element of the array, decrement the array length by 1, and return the value of the element it removed.
If the array is already empty, pop() does not change the array and returns an undefined value
For example:
Output result:
George,John,Thomas
Thomas
George, John
4. push() adds one or more elements to the end of the array and returns the new length
For example:
Output result:
George,John,Thomas
4
George,John,Thomas,James
5. unshift() adds one or more elements to the beginning of the array and returns the new length
For example:
Output result:
George,John,Thomas
4
James,George,John,Thomas
6. reverse() reverses the order of elements in the array
For example:
Output result:
George,John,Thomas
Thomas,John,George
7. shift() deletes and returns the first element of the array
For example:
Output result:
George,John,Thomas
George
John,Thomas
8. slice(start,end) returns the selected element from an existing array
Please note that this method does not modify the array, but returns a subarray
For example:
Output result:
George,John,Thomas
John,Thomas
George,John,Thomas
9. sort() sorts the elements of the array
A reference to the array. Please note that the array is sorted on the original array and no copy is generated
By default, this method sorts according to the order of character encoding (ASCII)
For example:
Let’s look at another example:
We can see that it is not sorted by numerical size as we think. If you want to sort by numerical size, you need to change the default sorting method and specify the sorting rules yourself.
As follows:
10. splice() deletes elements and adds new elements to the array
The splice() method has different functions from the slice() method. The splice() method will directly modify the array
(1) Delete array elements in the specified range:
(2) Insert the specified element starting from the specified subscript (the number of elements is not limited):
(3) Delete the array elements in the specified range and replace them with the specified elements (the number of elements is not limited):