A simple understanding of javascript-arrays

Source: Internet
Author: User
Tags javascript array

Group together (what is an array)

We know that variables are used to store data, and a variable can store only one content. Suppose you want to store 10 people's names or store 20 People's math scores, you need 10 or 20 variables to store, if you need to store more data, it will become more troublesome. We use arrays to solve problems, and an array variable can hold multiple data. Like a regiment, the regiment has a lot of people, as follows we use an array to store 5 student scores.

An array is a collection of values, each with an index number, starting at 0, with a corresponding value for each index, and adding more values as needed.

Group and take a name for the regiment (how to create an array)

The array is created first, and the array itself needs to be assigned to a variable. Like we travel, to group, and to set a name "Yunnan tour."

To create an array syntax:

var myarray=new Array ();


As we create arrays, we can also specify the length of the array, which can be arbitrarily specified.

Attention:
1. The new array created is an empty array with no value, such as output, to display undefined.
2. Although a length is specified when an array is created, the array is actually long, meaning that the element can still be stored outside the specified length even if the length is specified as 8

Who is the member of the Regiment (array assignment)

The array is created, and next we assign the values to the arrays. We put the array looks like the tour bus, the bus has a lot of locations, each location has a number, the customer to sit in which position?

Step one: Group A bus The second step: according        to the ticket seat of the bus number 1th is the Zhang San        bus 2nd seats is John Doe

How arrays are expressed:

First step: Create the array var myarr=new array (); The second step: assigning a value to        the array myarr[1]= "Zhang San";        myarr[2]= "John Doe";

An array is created below to store the math scores of 5 people.

var myarray=new Array (); Creates a new empty array myarray[0]=66; Store 1th person's achievement myarray[1]=80; Store 2nd person's achievement myarray[2]=90; Store 3rd person's achievement myarray[3]=77; Store 4th person's achievement myarray[4]=59; Store 5th person's grade

Note: The array has an index number for each value, starting with 0.

We can also create the above array and assignment in a simple way:

The first method:

var myarray = new Array (66,80,90,77,59);//create array to assign value at the same time

The second method:

var myarray = [66,80,90,77,59];//input an array directly (called "literal array")

Note: Data stored in an array can be of any type (numbers, characters, booleans, etc.)

Add a new member (adds a new element to the array)

In the previous section, we used the myarray variable to store 5 people's scores, now one more person's results, how to store it?

You can add new elements to an array at any time by simply using the next unused index.

myarray[5]=88; Adds a new element to an array using a new index
Note: Myarray.push (99); So it's not necessary to find out how many bits are the last
Call Regiment members (using array elements)

We know that each value in the array has an index number, starting with 0, for example, the MyArray variable stores scores for 6 people:

To get the value of an array element, simply reference the array variable and provide an index, such as:
The first person's performance is represented by:myarray[0]
The third person's performance is represented by:myarray[2]

Understanding the number of members (array property length)

If we want to know the size of the array, simply reference an attribute of the array length. The Length property represents the size of the array, which is the number of elements in the array.

Grammar:

Myarray.length; Get the length of the array myarray

Note: Since the index of an array always starts with 0, the upper and lower bounds of an array are: 0 and length-1, respectively. The length of an array is 5, and the upper and lower bounds of the arrays are 0 and 4, respectively.

var arr=[55,32,5,90,60,98,76,54];//contains an array of 8 values arr document.write (arr.length); Display array length 8document.write (arr[7]); Displays the value of the 8th element 54

At the same time, the length property of the JavaScript array is variable, which requires special attention.

arr.length=10; Increase the length of the array document.write (ARR.LENGTH); Array length has changed to 10

The length of the array changes as the element grows, as follows:

var arr=[98,76,54,56,76]; An array containing 5 values document.write (arr.length); Displays the length of the array 5arr[15]=34;  add element, use Index to 15, assign value to 34alert (arr.length); Displays the length of the array 16
Two-dimensional arrays

One-dimensional arrays, we look at a set of boxes, each box can only be one content.

Representation of one-dimensional arrays: myarray[]

Two-dimensional arrays, we look at a set of boxes, but each box can also put more than one box.

Representation of a two-dimensional array: myarray[[]

Note: The index values for the two dimensions of a two-dimensional array are also starting from 0, and the last index value for two dimensions is length-1.

1. How to define a two-D array

var myarr=new Array ();  First declare one-dimensional for (Var i=0;i<2;i++) {   //one-dimensional length of 2   myarr[i]=new Array ();  Re-declare the two-dimensional    for (Var j=0;j<3;j++) {   //two-D length is 3   myarr[i][j]=i+j;   Assignment, the value of each array element is i+j   }}


Note: For the FOR Loop statement, see Chapter Fourth 4-5.

The above two-dimensional array is represented in a tabular way:

2. Method of defining two-dimensional arrays two

var myarr = [[0, 1, 2],[1, 2, 3]]

3. Assigning values

myarr[0][1]=5; The value of 5 is passed into the array, overwriting the original value.

Description: Myarr[0][1], 0 represents the table row, and 1 represents the table column.

Practice:

Create an array
var arr = [' * ', ' # # ', ' * * * ', ' && ', ' * * * ', ' ##* '];

Note: He has created a two-dimensional array here.

A simple understanding of javascript-arrays

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.