JavaScript Learning Notes collation (array)

Source: Internet
Author: User

An array is a container that can store a set or series of related data.

first, why use arrays.

A. To solve the problem of storing and using large amounts of related data.

B. Simulation is really the world.

second, how to create an array

A. Create by Object-- var a=new Array ();

How to assign a value:

1. Direct Assignment--var a=new Array (element 1, Element 2, Element 3, Element 4,........)

var a=new Array (Numeric)
If there is only one element, and this element is a numeric type, then he is the length of the specified array and his value is undefined

var a=New Array (3); alert (a.length);    Results:3var a=New Array (3,2); alert (a.length);    Results:2

2. Declaration of value later

var a=New  Array (); a[0]=1; a[2]=2;  a[0]=3;

B. The way of stealth declaration--var a=[];

How to assign a value:

1. Direct Assignment:

var a=[1,2,3,4];  

2. Declaration of value later

var a=[];a[0]=1; a[1]=2;  a[2]=3;

JavaScript arrays can store values of any type
var a=new Array (3,true, ' abc ');

Iii. Accessing the elements of an array (accessed through an array of (brackets) subscript) array subscript starting from 0, his maximum value, is the Length property-1 Iv. iterating through the elements of an array
// For loop efficiency optimization var a=[1,2,3];  for (var i=0,max=a.length;i<max;i++) {    alert (a[i]);} // while (); var i=0;  while (i<a.length) {    alert (a[i]);    I+ +;} // For in (only a few arrays can be executed from the first one several times) inefficient  for (var in a) {    alert (a[i]);}

v. Classification of Arrays

A. Type of subscript

1. Subscript is a numeric type (indexed array)

var a=[];a[0]=1; a[1]=2; a[

2. Subscript is a string type (associative array)

var a=[];a[' name ']=2; alert (a.name);        Popup:2alert (a[' name ');    Eject: 2alert (a);                Popup: Empty

B. Dimensions to classify

A. One-dimensional arrays

var a=[1,2,3];

B. Two-d arrays

var arr=[[1,2,3],[4,5,6]];alert (arr[1][1])

Attention:

//can store any type of datavara=[1, ' name ',true, ' ABC '];//only one-dimensional arrays are supported. varA=[];a[0][0]=2Alert (a[0][0]); ErrorvarA=[[]];a[0][0]=2Alert (a[0][0]); Eject: 2//variable length. vara=[1,2];alert (a.length); Results:2a[4]=2; alert (a.length); Results:5//In the case of an indexed array, the subscript always starts at 0, and if the length is specified but is not assigned, his value is automatically assigned to undefinedvarA=[];a[3]=2; alert (a[2]);           Results: Undefinedalert (a.length); Results:4

JavaScript Learning Notes collation (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.