Javascript-array (Array)

Source: Internet
Author: User

Migration time: May 25, 2017 08:05:33

updatetime--2017 March 31 16:29:08

(b) Arrays (array)

1. Usage

//two kinds of ways to declare arrays in JS//First class (usually used)varArray1 = [];//InitializevarArray2 = [n/a];//declaring and assigning valuesARRAY2[2] = 4;//specifying the value of a fixed position array elementArray2.push (6);//1,2,4,6//Type IIvarArray3 =NewArray (); Array3.push ("Zhang San");varArray4 =NewArray (3);//declaring an array and specifying the lengthArray4[0] = "Zhang San"; Array4.push ("Name");//Zhang San,,, name

Attention:

   1. Two points different from Java:

     A.java can only assign values to specified array elements (JS can also add elements to an array via the push () method)

     B.java If you specify an array length, you cannot cross the array. (JS can)

   2.push () method description

     2.1. The declared array does not have a specified length

       2.1.1 If you do not assign a value to the specified element, the push () method adds the element, starting with the element labeled 0, and inserting it;

       2.1.2 If you assign a value to the specified element and then call the push () method to add the element, the insertion begins at the position of the specified element subscript +1;

Example:

var New Array (); array3[8] = ' AA ';    Array3.push ("Zhang San"); // ,,,,,,,, AA, Zhang San

    

2.2 When an array is declared, the length is specified

      The push () method is called to add an element, starting with the last element subscript +1 (that is, the subscript is the length n) where the insertion begins (that is, allow out of bounds);

Example:

var New Array (3); // declares an array and specifies the length array4[0] = "Zhang San"; Array4.push ("name"); // Zhang San,,, name

3. The first class of method differs from the second type of method in that the first class cannot initialize the array length, but it is not necessary to specify the length of the array, even if the length is specified, the array does not throw an exception when it is out of bounds

2. Modification and deletion of arrays

Add: Generally use the push () method, you can also use the specified element subscript to be added; change: Specify the element subscript to modify; Delete: Specifies that the value of the underlying element is null

Example:

var New Array (); Array.push ("Zhang San"); // Add Array.push ("John Doe"), Array.push ("Harry"), array[1] = "Zhao Liu"; // Modify null; // Delete

3. Traversing an array (check)

var New Array (); Array.push ("Zhang San"); Array.push ("John Doe"); Array.push ("Harry");
/**/for (var in array) {    console.log (Array[index]);}
/**/for (var i = 0; i < array.length; i++) {    Console.log (Array[i]);}

updatetime--2017 April 1 11:04:55

/**/$ (array). Each (function(index, value) {    console.log (value);});
/**/Array.foreach (function(value,index) {    Console.log ( value);    });

Attention:

  Although JavaScript can have its own Foreach method (method four) Like Java, it is not recommended to use the IE9 version. (If you have to use JS implementation, go to the article: JS custom method)

Method One, there is no such iterative way in Java.

Javascript-array (Array)

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.