join()
Put all elements of the array into a string. Elements are separated by the specified delimiter.
For example:
<script type="text/javascript"> var arr = ['item 1', 'item 2', 'item 3']; var list = '<ul><li>' + arr.join('</li><li>') + '</li></ul>'; </script>
list result:
'
pop()
Delete and return the last element of the array
The pop() method will delete the last element of the array and return the array to Decrements the length by 1 and returns 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:
<script type="text/javascript"> var arr = ["George", "John", "Thomas"]; document.write(arr + "<br/>"); document.write(arr.pop() + "<br/>"); document.write(arr); </script>
Output result:
George,John,Thomas
Thomas
George,John
push()
Add one or more elements to the end of the array and return the new length
For example:
<script type="text/javascript"> var arr = ["George", "John", "Thomas"]; document.write(arr + "<br/>"); document.write(arr.push("James") + "<br/>"); document.write(arr); </script>
Output result:
George,John,Thomas
4
George,John,Thomas,James
unshift()
Towards the beginning of the array Adds one or more elements and returns the new length
For example:
<script type="text/javascript"> var arr = ["George", "John", "Thomas"]; document.write(arr + "<br/>"); document.write(arr.unshift("James") + "<br/>"); document.write(arr); </script>
Output result:
George,John,Thomas
4
James, George, John, Thomas
shift()
Delete and return the first element of the array
For example:
<script type="text/javascript"> var arr = ["George", "John", "Thomas"]; document.write(arr + "<br/>"); document.write(arr.shift() + "<br/>"); document.write(arr); </script>
Output result:
George,John,Thomas
George
John,Thomas
##sort()
Sort the elements of the array
This method defaults to sorting in the order of character encoding (ASCII)
For example:
<script type="text/javascript"> var arr = new Array(6); arr[0] = "John"; arr[1] = "George"; arr[2] = "Thomas"; document.write(arr + "<br/>"); document.write(arr.sort()); </script>
Output result:
John,George,Thomas
George,John,Thomas
<script type="text/javascript"> var arr = new Array(6); arr[0] = 10 arr[1] = 5 arr[2] = 40 arr[3] = 25 arr[4] = 1000 arr[5] = 1 document.write(arr + "<br/>"); document.write(arr.sort()); </script>
10,5,40,25,1000,1
1,10,1000,25,40,5
is as follows:
<script type="text/javascript"> var arr = new Array(6); arr[0] = 10 arr[1] = 5 arr[2] = 40 arr[3] = 25 arr[4] = 1000 arr[5] = 1 document.write(arr + "<br/>"); document.write(arr.sort(function (a, b) {return a - b;}));// 从大到小 </script>
10,5,40,25,1000,1
1,5,10,25,40,1000
If you want descending order What about the arrangement?
Change the sorting rule to:
function (a, b) {return b - a;}
That’s OK
The above is the detailed content of Summary of usage examples of array processing functions of basic JavaScript functions. For more information, please follow other related articles on the PHP Chinese website!