var arr = ['item 1', 'item 2', 'item 3']; var list = '
Home > Article > Web Front-end > Summary of usage examples of array processing functions of basic JavaScript functions
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:
'ff6d136ddc5fdfeffaf53ff6ee95f18525edfb22a4f469ecb59f1190150159c6item 1bed06894275b65c1ab86501b08a632eb25edfb22a4f469ecb59f1190150159c6item 2bed06894275b65c1ab86501b08a632eb25edfb22a4f469ecb59f1190150159c6 item 3bed06894275b65c1ab86501b08a632eb929d1f5ca49e04fdcb27f9465b944689'
This is by far the fastest method! Using native code (such as join()), regardless of what the system does internally, is usually much faster than non-native. ——James Padolsey, james.padolsey.com
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>Output result:
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>Output results:
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!