An introductory tutorial on Javascript array usage

Source: Internet
Author: User
Tags array length arrays join javascript array

Defining arrays

The code is as follows Copy Code

var a = new Array ("A", "B", "C");

Method 1.

var mycars=new Array ()
mycars[0]= "Saab"
mycars[1]= "Volvo"
mycars[2]= "BMW"

Method 2.

Define and initialize together:

The code is as follows Copy Code

var mycars=new Array ("Saab", "Volvo", "BMW")

Or another way of writing:

The code is as follows Copy Code

var mycars=["Saab", "Volvo", "BMW"];


Looping Read

  code is as follows copy code

for (i = 0; i < a.length i++) {//circular read
document.write (i + ":" + a[i] + "<br/>");
}

document.write ("toString ():" + a.tostring () + "<br/>");//tostring (), array in string representation
document.write ("Join ():" + A.join ("") + "<br/>");//join (), with the string argument after the join as the spacer, generates a string, if the argument is a half-width comma, the effect and ToString ( ) The same
document.write ("reverse ():" + a.reverse (). toString () + "<br/>"); Generate a new array of elements in the array in reverse
document.write ("valueof ():" + a.valueof () + "<br/>");//valueof (), Output array value


Two-dimensional array

The code is as follows Copy Code
var b = new Array (3);//define an array of length 3
B[0] = new Array ("a", 1);
B[1] = new Array ("B", 2);
B[2] = new Array ("C", 3);
for (i = 0; i < b.length; i++) {//loop output each element of each dimension
for (j = 0; J < B[i].length; J + +) {
document.write ("b[" + i + "] [" + j + "]:" + b[i][j] + "<br/>");
}
}

Array Length:

The JavaScript array does not need to be set in length, it expands itself, and the array name. Length returns the number of elements


The following is an example of changing the length property:

  code is as follows copy code

var arr=[ 12,23,5,3,25,98,76,54,56,76];
//defines a 10-digit array
alert (arr.length); //Displays the length of the array
arr.length=12;  //increase the length of the array
alert ( Arr.length); //display the length of the array has changed to

Alert (arr[8]);  //Display the value of the 9th element,
arr.length=5;   Reduces the length of the array to 5, and the elements indexed equal to or more than 5 are discarded
alert (arr[8]);  //display 9th element has changed to "undefined"
arr.length=10;   //restore array length to
Alert (arr[8]);  //Although the length is restored to 10, the 9th element cannot be retracted, displaying "undefined"

From the above code we can clearly see the nature of the length property. But the length object can be set not only explicitly, it may also be implicitly modified. You can use a variable that is not declared in JavaScript, and you can use an undefined array element (an element whose index exceeds or equal to length), at which point the value of the length property is set to the value plus 1 for the element index used. For example, the following code:

The code is as follows Copy Code
var arr=[12,23,5,3,25,98,76,54,56,76];
alert (arr.length);
arr[15]=34;
alert (arr.length);

The code also first defines an array of 10 digits, which can be seen by an alert statement of 10. Then the element with index 15 is assigned to 15, or arr[15]=34, and then the length of the array is output by the alert statement, with 16. In any case, this is a surprising feature for developers who are accustomed to strongly typed programming. In fact, an array created with the new Array () has an initial length of 0, and it is an operation that does not define an element in it, which changes the length of the array.
As you can see from the above introduction, the length property is so magical that it makes it easy to increase or decrease the size of the array. Therefore, a thorough understanding of the length attribute is helpful to the flexible application in the development process.

Attached message:
Supnate: There is no specific example, and it is understood that the length property understands the array in JavaScript. Arrays are the basis for many examples. Because it is quite different from other languages, so speak it alone. For example, to implement a pop method for arrays that are not supported by a low version browser:

The code is as follows Copy Code
Array.prototype.pop=function () {
if (0==this.length) return;
var temp=this[this.length-1];
this.length--;
return temp;
}

So the array can be used as a stack.

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.