JavaScript Array manipulation functions

Source: Internet
Author: User
Tags javascript array

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:

<script type="Text/javascript">vararr = [1,2,3]; varARR1 = [ One, A, -]; document.write (Arr.concat (4,5, arr1)); </script>Output Result:1,2,3,4,5, One, A, -
2. Join ()

Put all the elements of the array into a string. element is delimited by the specified delimiter.

For example:

<script type="Text/javascript">vararr = ['Item 1','Item 2','Item 3']; varList ='<ul><li>'+ Arr.join ('</li><li>') +'</li></ul>';</script>list result: '<ul><li>item1</li><li>item2</li><li>item3</li></ul> '
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:

Script Type="Text/javascript">vararr = ["George","John","Thomas"]; document.write (arr+"<br/>"); document.write (Arr.pop ()+"<br/>"); document.write (arr); </script>
Output Result: George,john,thomasthomasgeorge,john
4. Push () adds one or more elements to the end of the array and returns the new length

For example:

<script type="Text/javascript">vararr = ["George","John","Thomas"]; document.write (arr+"<br/>"); document.write (Arr.push ("James") +"<br/>"); document.write (arr);</script>output Result: George,john,thomas4George,john,thomas,james
5, Unshift () adds one or more elements to the beginning of the array and returns the new length

For example:

<script type="Text/javascript">vararr = ["George","John","Thomas"]; document.write (arr+"<br/>"); document.write (Arr.unshift ("James") +"<br/>"); document.write (arr); </script>output Result: George,john,thomas4James,george,john,thomas
6, reverse () reverses the order of the elements in the array

For example:

<script type="text/javascript">       var arr = ["George " " John " " Thomas " ];       " <br/> " );        </script> Output Result: George,john,thomasthomas,john,george
7, Shift () Delete and return the first element of the array

For example:

<script type="Text/javascript">vararr = ["George","John","Thomas"]; document.write (arr+"<br/>"); document.write (Arr.shift ()+"<br/>"); document.write (arr);</script>output Result: George,john,thomasgeorgejohn,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:

Script Type="Text/javascript">vararr = ["George","John","Thomas"]; document.write (arr+"<br/>"); document.write (Arr.slice (1) +"<br/>"); //truncate to the end of the array starting with the first elementdocument.write (arr);</script>output Result: George,john,thomasjohn,thomasgeorge,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:

<script type="Text/javascript">vararr =NewArray (6); arr[0] =Tenarr[1] =5arr[2] = +arr[3] = -arr[4] = +arr[5] =1document.write (arr+"<br/>");d Ocument.write (Arr.sort ()); </script>Output Result:Ten,5, +, -, +,11,Ten, +, -, +,5
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);//Delete the third element after the three array elements (containing the third element) 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" 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 third element after the three array elements (including the third element), and with "William", "JACK" to replace the document.write (arr); </script>

Output Result:

George,john,thomas,james,adrew,martin
George,john,william,jack,martin

JavaScript Array manipulation functions

Related Article

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.