Summary of JS array processing function

Source: Internet
Author: User

Original: JS array processing function Summary

1, concat () connect two or more arrays
The method does not alter the existing array, but only returns a copy of the concatenated array.
For example:

1 <script type= "Text/javascript" >2        var arr = [1, 2, 3]; 3        var arr1 = [One, one, one]; 4        document.write (Arr.concat (4, 5, arr1)); 5 </script>

Output Result:
1,2,3,4,5,11,22,33

2. Join ()
Put all the elements of the array into a string. element is delimited by the specified delimiter.
For example:

1 <script type= "Text/javascript" >2       var arr = [' Item 1 ', ' Item 2 ', ' Item 3 '];
    3       var list = ' <ul><li> ' + arr.join (' </li><li> ') + ' </li></ Ul> '; 4 </script>

List results:

' <ul><li>item 1</li><li>item 2</li><li>item 3</li></ul> '
This is by far the quickest method! Using native code, such as join (), is usually much faster than non-native, regardless of what is done inside the system. --james Padolsey, james.padolsey.com

3, pop () Delete and return the last element of the array
The Pop () method removes the last element of the array, reducing the length of the array by 1 and returning the value of the element it deletes.
If the array is already empty, the pop () does not change the array and returns the undefined value
For example:

1 <script type= "Text/javascript" >2       var arr = ["George", "John", "Thomas"]; 3       document.write (arr + "<br/>"); 4       document.write (Arr.pop () + "<br/>"); 5       document.write (arr); 6 </script>

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:

1 <script type= "Text/javascript" >2       var arr = ["George", "John", "Thomas"]; 3       document.write (arr + "<br/>"); 4       document.write (Arr.push ("James") + "<br/>"); 5       document.write (arr); 6 </script>

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:

1 <script type= "Text/javascript" >2       var arr = ["George", "John", "Thomas"]; 3       document.write (arr + "<br/>"); 4       document.write (Arr.unshift ("James") + "<br/>"); 5       document.write (arr); 6 </script>

Output Result:
George,john,thomas
4
James,george,john,thomas

6, reverse () reverses the order of the elements in the array
For example:

1 <script type= "Text/javascript" >2       var arr = ["George", "John", "Thomas"]; 3       document.write (arr + "<br/>"); 4       document.write (Arr.reverse ()); 5 </script>

Output Result:
George,john,thomas
Thomas,john,george

7, Shift () Delete and return the first element of the array
For example:

1 <script type= "Text/javascript" >2       var arr = ["George", "John", "Thomas"]; 3       document.write (arr + "<br/>"); 4       document.write (Arr.shift () + "<br/>"); 5       document.write (arr); 6 </script>

Output Result:
George,john,thomas
George
John,thomas

8, Slice (start,end) returns the selected element from an existing array
Note that the method does not modify the array, but instead returns a Subarray
For example:

1 <script type= "Text/javascript" >2       var arr = ["George", "John", "Thomas"]; 3       document.write (arr + "<br/>"); 4       // truncate to the end of the array starting with the first element 5       document.write (arr); 6 </script>

Output Result:
George,john,thomas
John,thomas
George,john,thomas

9. Sort () Sorts the elements of an array
A reference to an array. Note that the array is sorted on the original array and no replicas are generated
The method is sorted by default in the order of character encoding (ASCII).
For example:

1 <script type= "Text/javascript" >2     varnew Array (6); 3     Arr[0] = "John"; 4     ARR[1] = "George"; 5     ARR[2] = "Thomas"; 6     document.write (arr + "<br/>"); 7     document.write (Arr.sort ()); 8 </script>

Output Result:
John,george,thomas
George,john,thomas

Let's look at one more example:

1 <script type= "Text/javascript" > 2     varnew Array (6); 3     Arr[0] = 4     arr[1] = 5 5     arr[2] = 6     arr[3] = 7     arr[4] = 8     arr[5] = 1 9     document.write (arr + "<br/>"); Ten     document.write (Arr.sort ());  </script>

Output Result:
10,5,40,25,1000,1
1,10,1000,25,40,5

As we can see, it's not sort by number we think, if you want to sort by numeric size, you need to change the default sorting method and specify the collation yourself.
As follows:

1<script type= "Text/javascript" >2     vararr =NewArray (6);3Arr[0] = 104ARR[1] = 55ARR[2] = 406ARR[3] = 257ARR[4] = 10008ARR[5] = 19document.write (arr + "<br/>");Tendocument.write (Arr.sort (function(A, B) {returnA-B;}));//from big to small One</script>

Output Result:
10,5,40,25,1000,1
1,5,10,25,40,1000
What if you want to sort in descending order?
Change the collation to:
function (A, b) {return b-a;}
It's OK.

10. Splice () Delete the element and add a new element to the array
The splice () method is different from the slice () method, and the splice () method modifies the array directly
(1) Delete the array element of the specified range:

1<script type= "Text/javascript" >2     vararr =NewArray (6);3Arr[0] = "George"; 4ARR[1] = "John";5ARR[2] = "Thomas";6ARR[3] = "James";7ARR[4] = "Adrew";8ARR[5] = "Martin";9 Tendocument.write (arr + "<br/>"); OneArr.splice (2, 3);//three array elements after the third element is removed (contains the third element) A document.write (arr); -</script>

Output Result:
George,john,thomas,james,adrew,martin
George,john,martin


(2) Insert the specified element (unlimited number of elements) starting from the specified subscript:

1<script type= "Text/javascript" >2    vararr =NewArray (6);3Arr[0] = "George";4ARR[1] = "John";5ARR[2] = "Thomas";6ARR[3] = "James";7ARR[4] = "Adrew";8ARR[5] = "Martin";9 Tendocument.write (arr + "<br/>"); OneArr.splice (2, 0, "William", "JACK");//Insert "William" before the third element, "JACK" A document.write (arr); -</script>

Output Result:
George,john,thomas,james,adrew,martin
George,john,william,jack,thomas,james,adrew,martin


(3) Delete the array element of the specified range and replace it with the specified element (unlimited number of elements):

1<script type= "Text/javascript" >2    vararr =NewArray (6);3Arr[0] = "George";4ARR[1] = "John";5ARR[2] = "Thomas";6ARR[3] = "James";7ARR[4] = "Adrew";8ARR[5] = "Martin";9 Tendocument.write (arr + "<br/>"); OneArr.splice (2,3, "William", "JACK");//Delete the next three array elements of the third element (containing the third element) and replace it with "William", "JACK" A document.write (arr); -</script>

Output Result:
George,john,thomas,james,adrew,martin
George,john,william,jack,martin

Summary of JS array processing function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.