JS Array definition

Source: Internet
Author: User
Tags array definition

JS Array definition
Foreign sites to get down some of the JS array operation implementation, this tutorial is mainly defined in this regard.
var arr = new Array ();
var arr = new Array ([length]);
var arr = [Element1, Element2, ..., ELEMENTN];

In both cases the ARR variable is a array that has three element:a, B, C
var arr = new Array ("A", "B", "C");
Or
var arr = ["A", "B", "C"];

You can access the elements of the array by zero-based indices
var firstitem = arr[0]; A
var thirditem = arr[2]; C

You can get the length of the ' array by the ' Length property
var len = arr.length; 3

Example Two

var arr1 = new Array ("A", "B", "C");
var arr2 = new Array (1, 2, 3);
var Multiarr = new Array (arr1, ARR2);
Or
var Multiarr = [arr1, arr2];
Or
var multiarr = [["A", "B", "C"], [1, 2, 3]];

You can access the elements of the array by zero-based indices
var firstrow = multiarr[0]; Same as Arr1
var Secondrowfirstcell = multiarr[1][0]; 1

Three
var arr = ["A", "B", "C"];

Display the elements of the array (ABC)
for (var i = 0; i < arr.length; i++) {
document.write (Arr[i]);
}

Iterating with the for in statement, I'll be 0, 1, 2.
The output would be ABC
for (var i in arr) {
document.write (Arr[i]);
}
Four
We must create an array:
var arr = [];
Add elements at the position:
Arr[0] = "A";
ADD elements at the second position:
ARR[1] = "B";
Adding an element to the end of the array:
Arr.push ("C");
Now the ARR variable is a array that have three elements:a,b,c.

Five

var arr1 = ["A", "B", "C"];
var arr2 = [1, 2, 3];
Concat the arrays:
var newArr = Arr1.concat (ARR2);

Six

var arr = ["A", "B", "C"];
document.write (arr); Output:a,b,c

Arr.splice (1, 1);
document.write ("<br/>");
document.write (arr); Output:a,c

Seven
var arr = {};
Can add elements to a associative array in two different ways:
arr["Name" = "John";
arr["location"] = "Utah";
Second Way
Arr.age = 13;

You can access the elements of this array by string indices
var loc = arr["Location"]; "Utah"
Or like a
var loc = arr.location; "Utah"

You are cannot get the length of the array via the
It contains an invalid value
var len = arr.length; Len would

Eight
var arr = {};
arr["Name" = "John";
arr["location"] = "Utah";
Arr["age"] = 13;

Iterating with the for in statement, I'll be ' name ', ' Location ', ' age '.
The output would be JohnUtah13
for (var i in arr) {
document.write (Arr[i]);
}

Nine

var arr = {};
arr["Name" = "John";
arr["location"] = "Utah";
Arr["age"] = 13;
Display the initial contents of the array, Output:johnutah13
for (var i in arr) {
document.write (Arr[i]);
}

Delete arr.location; Delete the Location property from the array
document.write ("<br/>");
Display the current contents of the array, output:john13
for (var i in arr) {
document.write (Arr[i]);
}

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.