JS Array (array) processing function Collation _ basic knowledge

Source: Internet
Author: User

1, concat () to connect two or more arrays
This method does not change an existing array, but merely returns a copy of the connected array.
For example:

Copy Code code as follows:

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

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

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

Copy Code code as follows:

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

List results:

' <ul><li>item 1</li><li>item 2</li><li>item 3</li></ul> '
This is by far the quickest way! The use of native code (such as Join ()) is usually much faster than a 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 deletes 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:

Copy Code code as follows:

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

Output results:
George,john,thomas
Thomas
George,john

4. Push () adds one or more elements to the end of the array and returns a new length
For example:

Copy Code code as follows:

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

Output results:
George,john,thomas
4
George,john,thomas,james

5, Unshift () adds one or more elements to the beginning of the array, and returns a new length
For example:

Copy Code code as follows:

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

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

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

Copy Code code as follows:

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

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

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

Copy Code code as follows:

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

Output results:
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 child array
For example:

Copy Code code as follows:

<script type= "Text/javascript" >
var arr = ["George", "John", "Thomas"];
document.write (arr + "<br/>");
document.write (Arr.slice (1) + "<br/>"); Intercept from first element to end of array
document.write (arr);
</script>

Output results:
George,john,thomas
John,thomas
George,john,thomas

9, sort () sorting elements of the array
The reference to the 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:

Copy Code code as follows:

<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 results:
John,george,thomas
George,john,thomas

Let's look at one more example:

Copy Code code 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 ());
</script>

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

We can see that instead of sorting by number size as we think, if you want to sort by number size, you need to change the default sort method and specify the collation yourself.
As follows:

Copy Code code 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;}); /From Big to small
</script>

Output results:
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 elements, and add new elements to the array
The splice () method is different from the slice () method, and the splice () method is modified directly to the array
(1) Deletes the array element of the specified range:

Copy Code code as follows:

<script type= "Text/javascript" >
var arr = new Array (6);
Arr[0] = "George";
ARR[1] = "John";
ARR[2] = "Thomas";
ARR[3] = "James";
ARR[4] = "Adrew";
ARR[5] = "Martin";
document.write (arr + "<br/>");
Arr.splice (2, 3); Deletes the three array elements after the third element (contains the third element)
document.write (arr);
</script>

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

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

Copy Code code as follows:

<script type= "Text/javascript" >
var arr = new Array (6);
Arr[0] = "George";
ARR[1] = "John";
ARR[2] = "Thomas";
ARR[3] = "James";
ARR[4] = "Adrew";
ARR[5] = "Martin";
document.write (arr + "<br/>");
Arr.splice (2, 0, "William", "JACK"); Insert "William" before the third element, "JACK"
document.write (arr);
</script>

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


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

Copy Code code as follows:

<script type= "Text/javascript" >
var arr = new Array (6);
Arr[0] = "George";
ARR[1] = "John";
ARR[2] = "Thomas";
ARR[3] = "James";
ARR[4] = "Adrew";
ARR[5] = "Martin";
document.write (arr + "<br/>");
Arr.splice (2,3, "William", "JACK"); Deletes the three array elements (containing the third element) after the third element and replaces them with "William" and "JACK"
document.write (arr);
</script>

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

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.