JS array objects and array common properties and methods

Source: Internet
Author: User

Definition form:

var arr1 = new Array (1, 5, 8, 7, 2, 10); Defines an array with 6 of data

var arr2 = new Array (); It simply defines an array (name), but does not give the value (data), which is now empty

var arr3 = [1, 5, 8, 7, 2, 10]; With ARR1, it is just a shorthand definition method.

var arr4 = []; Same as ARR2, also an empty array.

Another form of array traversal is the--for in loop statement.

for (Var  v1   in  array name arr1) {         //Here is the loop body, which is a traversal loop specifically for the array arr1, where the value of V1 is the subscript value representing each item of the array.         //v1 is just a "temporary variable" that represents each subscript, which in turn changes from 0 to the maximum subscript of the array. }

Property:

Arr.length: Indicates the length of the array object

Method:

Arr.concat (Other array): joins two arrays to become a new "longer" array.
Vars1 = V1.concat (v2); At this point S1 is such an array: [2, 5, 1, 5, 5, 1, 6, 8];

Arr.join ("string"): All items in the array are "strung together" into "long" strings with the specified characters.

var s2 = v1.join ("//"); Result S2 As string "2//5//1//5"

Arr.pop (); Removes the last item of the array (delete) and returns the item data, that is, the array is missing one item

var s3 = V1.pop (); The result v1 only this: [2,5,1]; The value of S3 is 5.

Arr.push (new data item D1); Adds a new data D1 to the last position of the array, that is, the array has one more item.

var S4 = V1.push (55); V1 at this point: [2,5,1, +], the value of S4 is the length of the new array, that is, 4

Arr.shift (); "Remove" the first item of the array (delete) and return the item data, that is, the array is missing one item

var S5 = V1.shift (); The result v1 only this: [5, 1,55]; The value of S5 is 2.

Arr.unshift (new data item D1); Adds a new data D1 to the first position of the array, that is, the array has one more item.

var V6 = V1.unshift (66); V1 at this time: [5, 1, S6], the value of the new array is the length, that is, 4

 

Property

Length

Method

Concat ()

var arr = Arr1.concat (ARR2); ARR1 = [ARR2] = [4,5,6] At this time arr is [1,2,3,4,5,6]

Join ()

Concatenate the array with the specified characters into a string arr.join (' | ');

Pop ()

Remove the last item of the divisor Arr.pop ();

Push ()

Add a new cell Arr.push (' abc ') at the end of the array;

Shift ()

Move the first item of an array and return as a result Arr.shift ()

Unshift ()

Add new data to the front of the array, Arr.unshift (' ad ');

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.