javascript--Array (ii)

Source: Internet
Author: User

Five. Addition and deletion of array elements

the simplest way to add an array element: Assign a value to a new index

a = [];//Start is an empty array

A[0] = "one";//add elements to it;

You can also use the push () method to add one or more elements to the end of the array:

a = [];//Start is an empty array

A.push ("one");//Add an element at the end of a = ["a"]

A.push ("Two", "three");//Continue adding two elements a = ["One", "two", "three"]

push () increments the element at the end of the array, the Unshift () method inserts the element at the header of the array, and moves the other elements to a higher index in turn.

 Deleting an array element uses the delete operator just as you would delete an object property:

A = [n/a];

Delete a [1]; A does not have an element in the position of index 1, but the length remains the 3,delete operator and does not affect array lengths

Deleting an array element is similar to assigning a undefined value (with subtle differences), and it is important to note that using delete on an array does not modify the length property of the arrays.

It also does not move the element from the high index to fill the whitespace left by the deleted attribute (the Shift () method will). If you delete an element from an array, it becomes a sparse array.

Six. Array traversal

  Using a For loop is the most common way to iterate over an array element:

Seven. Multidimensional arrays

  JS does not support a true multidimensional array, but it can be approximated by an array of arrays. Access the elements in an array of arrays, as long as you use the [] operator two times.

Create a multidimensional array

var table = new Array (10)//table has 10 rows

for (var i = 0; i < table.length; i++)

table[i] = new Array (10); Each row has 10 columns

Initializing an array

for (var row = 0; Row < Table.length; row++) {

for (col = 0; col < table[row].length; col++) {

Table[row][col] = row * COL;

}

}

Use multidimensional arrays to calculate (query) 5*7

var product = table[5][7]; 35

  

javascript--Array (ii)

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.