JS Array processing function arrangement, jsarray
1. concat () connects two or more Arrays
This method does not change the existing array, but only returns a copy of the connected array.
For example:
Copy codeThe Code is 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 result:
1, 2, 3, 4, 5, 11, 33
2. join ()
Put all elements of the array into a string. The elements are separated by the specified delimiter.
For example:
Copy codeThe Code is 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 result:
'<Ul> <li> item 1 </li> <li> item 2 </li> <li> item 3 </li> </ul>'
This is the fastest way so far! Using native code (such as join () is usually much faster than non-native code, no matter what is done inside the system. -- James Padolsey, james.padolsey.com
3. pop () deletes and returns the last element of the array.
The pop () method deletes the last element of the array, reduces the length of the array by 1, and returns the value of the deleted element.
If the array is empty, pop () does not change the array and returns the undefined value.
For example:
Copy codeThe Code is 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 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:
Copy codeThe Code is 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 result:
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 codeThe Code is 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 result:
George, John, Thomas
4
James, George, John, Thomas
6. reverse the order of elements in the array by reverse ()
For example:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var arr = ["George", "John", "Thomas"];
Document. write (arr + "<br/> ");
Document. write (arr. reverse ());
</Script>
Output result:
George, John, Thomas
Thomas, John, George
7. shift () deletes and returns the first element of the array.
For example:
Copy codeThe Code is 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 result:
George, John, Thomas
George
John, Thomas
8. slice (start, end) returns the selected element from an existing array
Note that this method does not modify the array, but returns a child array.
For example:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var arr = ["George", "John", "Thomas"];
Document. write (arr + "<br/> ");
Document. write (arr. slice (1) + "<br/>"); // truncate from the first element to the end of the array
Document. write (arr );
</Script>
Output result:
George, John, Thomas
John, Thomas
George, John, Thomas
9. sort the elements of the array by sort ().
Array reference. Please note that arrays are sorted on the original array without generating copies
This method is sorted by character encoding (ASCII) by default.
For example:
Copy codeThe Code is 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 result:
John, George, Thomas
George, John, Thomas
Let's look at an example:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var arr = new Array (6 );
Arr [0] = 10
Arr [1] = 5
Arr [2] = 40
Arr [3] = 25
Armr [4] = 1000
Arr [5] = 1
Document. write (arr + "<br/> ");
Document. write (arr. sort ());
</Script>
Output result:
We can see that it is not sorted by number size as we think. If you want to sort by number size, you need to change the default sorting method and specify the sorting rules by yourself.
As follows:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var arr = new Array (6 );
Arr [0] = 10
Arr [1] = 5
Arr [2] = 40
Arr [3] = 25
Armr [4] = 1000
Arr [5] = 1
Document. write (arr + "<br/> ");
Document. write (arr. sort (function (a, B) {return a-B;}); // from large to small
</Script>
Output result:
What if you want to sort them in descending order?
Change the sorting rule:
Function (a, B) {return B-;}
OK.
10. splice () deletes elements and adds new elements to the array.
The splice () method and slice () method have different functions. The splice () method directly modifies the array.
(1) Delete the array elements in the specified range:
Copy codeThe Code is 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); // Delete the three array elements after the third element (including the third element)
Document. write (arr );
</Script>
Output result:
George, John, Thomas, James, Adrew, Martin
George, John, Martin
(2) Insert a specified element from the specified subscript (the number of elements is unlimited ):
Copy codeThe Code is 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", "JACK" before the third element"
Document. write (arr );
</Script>
Output result:
George, John, Thomas, James, Adrew, Martin
George, John, William, JACK, Thomas, James, Adrew, Martin
(3) Delete the array elements in the specified range and replace them with the specified elements (the number of elements is unlimited ):
Copy codeThe Code is 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"); // Delete the three array elements (including the third element) after the third element, and use "William ", "JACK" to replace
Document. write (arr );
</Script>
Output result:
George, John, Thomas, James, Adrew, Martin
George, John, William, JACK, Martin