var arr = ['item 1', 'item 2', 'item 3']; var list = ''; &"/> 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

Summary of usage examples of array processing functions of basic JavaScript functions

伊谢尔伦
伊谢尔伦Original
2017-07-25 15:49:301346browse

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 = [&#39;item 1&#39;, &#39;item 2&#39;, &#39;item 3&#39;];
       var list = &#39;<ul><li>&#39; + arr.join(&#39;</li><li>&#39;) + &#39;</li></ul>&#39;;
 </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

Reference to the array. Please note that the array is sorted on the original array and no copy is generated

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

Let’s look at another example:

<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

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. , specify the sorting rules yourself.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn