Introduction to JS arrays-js definition Array

Source: Internet
Author: User

The article is mainly about how to define the array, as well as the dynamic modification of the length of the data.

JS Array Assignment

var myarray = [];
Myarray[myarray.length] = ' new element ';

Modify JS Array length

var myarray = [1,2,3];
Myarray.length//3
Myarray.length = 2; Delete the last element
Myarray.length =//Add elements to the array; The elements have the undefined value.

Defining arrays


An array object is used to store a series of values in a separate variable name.

We use the keyword new to create an array object. The following code defines an array object named MyArray:

The Var myarray=new array () has two methods of assigning values to an array (you can add as many values as you want, as you can define any number of variables you need).

1:

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

You can also use an integer argument to control the size of an array:

var mycars=new array (3)
Mycars[0]= "Saab"
mycars[1]= "Volvo"
mycars[2]= "BMW" 2:
var mycars=new Array ("Saab", "Volvo", "BMW")

Look at a definition of a two-dimensional array

<input value= "[[' A0 ', ' A1 '],[' B0 ', ' B1 '],[' C0 ', ' C1 ']]" "Name=" ss "Size=" "/><button onclick=" tt () "> Lést ' et</button>
<script>
function tt () {
Eval ("var aaa=" +ss.value);
For (I in AAA) for (J in Aaa[i]) a.innerhtml+= "aaa[" +i+ "[" +j+ "] = =" +aaa[i][j]+ "<br/>;}
</script>
<br/><label id= "a" ></label>

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.