Common Operations on JavaScript Arrays

Source: Internet
Author: User

In my first article, I want to develop the habit of writing a technical blog and ensure that at least one article is written every week on average. Code-related testing is required.

1. Create an array
Only the array is declared and does not contain specific data.
Java code
Var checkboxlist = new array ();

Create an array and specify the length. The length of 5 can be changed and can be automatically increased.
Java code
Var checkboxlist = new array (5 );

Creates an array and initializes three elements.
Java code
Var checkboxlist = new array ("1111", "2222", "3333 ");

2. Array Operations
2.1 Add an element at the end of the push () array and return the latest length of the array.

Put the string "userid001" at the end of the array
Java code
Checkboxlist. push ("userid001 ");

Add two elements at a time, "aaaa" and "bbbb"
Java code
Checkboxlist. push ("aaaa", "bbbb ");

2.2 unshift () array beginning with insert element, original element removed later
Insert two elements at the beginning of the array.
(Some materials say that the returned value is the latest length of the array, but I tested in ie6 and returned "undefined ")
Java code
Checkboxlist. unshift ("aaaa", "bbbb ");

2.3 pop () removes an element at the end of the array and returns the element.
Code:
Java code
Checkboxlist. pop ();

2.3 shift () removes an element starting with an array and returns the element
Code:
Java code
Checkboxlist. shift ()

2.4 splice () insert (replace) or remove multiple elements
Removal example:
Splice (delindex, delcount );
Delindex: the position where the removal starts, that is, the subscript of the array, starting from 0.
Delcount: number of elements to be removed.
Remove two elements from the first element of the array.
Java code
Checkboxlist. splice (0, 2 );

For example, after checkboxlist. splice (1111) is executed, the array is changed from the original four elements to only "4444" and.
Java code
Var checkboxlist = new array (); checkboxlist. push ("1111"); checkboxlist. push ("2222"); checkboxlist. push ("3333"); checkboxlist. push ("4444"); // remove two elements from the second element of the array. Checkboxlist. splice (1, 2); alert (checkboxlist );

Insert (replace) Example:
Splice (addindex, replacecount, elemim ...);
Addindex: the starting position of insert (replace), that is, the subscript of the array, starting from 0.
Replacecount: number of elements to be replaced.
Elememts...: new elements can be one or more.

When replacecount is less than or equal to 0, add only.
For example, insert "aaaa" and "bbbb" elements after the 1st elements ("1111") in the checkboxlist array.
Java code
Var checkboxlist = new array ("1111", "2222", "3333", "4444", "5555"); checkboxlist. splice (1, 0, "aaaa", "bbbb"); alert (checkboxlist );

When replacecount is greater than 0, the element is replaced.
For example, replace an element (that is, "1st") after the 1111 elements ("2222") of the checkboxlist array with "aaaa" and "bbbb ". The latest checkboxlist value in the following code is: "1111", "aaaa", "bbbb", "3333", "4444", "5555"
Java code
Var checkboxlist = new array ("1111", "2222", "3333", "4444", "5555"); checkboxlist. splice (1, 1, "aaaa", "bbbb"); alert (checkboxlist );

If the value of the replacecount parameter is greater than the length of the array, all elements starting with the addindex parameter are replaced. After execution, the element of the checkboxlist becomes: "1111", "aaaa", "bbbb"
Java code
Var checkboxlist = new array ("1111", "2222", "3333", "4444", "5555"); checkboxlist. splice (1,9, "aaaa", "bbbb"); alert (checkboxlist );

2.5 reverse order of elements in the reverse () array
Java code
Checkboxlist. reverse ();

2.6 sort () sorts array elements in the natural order
Java code
Checkboxlist. sort ();

2.7 concat () copies another array, generates a new array, and returns
The elements in alllist are the collection of elements in checkboxlist and radiolist.
Java code
Var checkboxlist = new array ("1111", "2222"); var radiolist = new array ("aaaa", "bbbb"); var alllist = checkboxlist. concat (radiolist );

2.8 slice () copies the element after the specified serial number, generates a new array, and returns
Slice (index)
Index: the start position of the copy, that is, the subscript of the array, starting from 0.
If the index is greater than the number of array elements, an empty array is returned.

Copy all elements after the first element of the checkboxlist to the new temparray. The original checkboxlist does not change.
Java code
Var checkboxlist = new array ("1111", "2222", "3333"); var temparray = checkboxlist. slice (1 );

2.9 join () string operation
Join (separator );
Separator: the added separator, which can be a "" null string.

It can be added to an efficient string. It is much more efficient than directly adding strings.
Sample Code:
Java code
Var checkboxlist = new array ("1111", "2222", "3333", "4444"); var strdata = checkboxlist. join ("|"); alert (strdata );

 

2.10 length attribute
Length returns the length of the array, that is, the number of elements.
Java code
Var checkboxlist = new array ("1111", "2222", "3333", "4444"); alert (checkboxlist. length );

Author's "Technical Summary"
 

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.