Javascript: Read and Write of array elements

Source: Internet
Author: User

You can use the [] syntax to read an array element or write an array element. We already know that the [] operator can be used to access the named attributes of an object: My ['salary '] * = 2;

Because an array is a special object, you can define non-numeric Object Attributes on an array and access it using the. or [] syntax.

① Add new elements

Javascript Arrays can have any number of elements and change the number of elements at any time. To add a new element to an array, you only need to assign a value to it.

 
A [10] = 10;

Arrays in JavaScript are sparse. This means that the subscript of the array does not need to fall into a continuous number range. Only elements that are actually stored in the array can be allocated to the memory by JavaScript. Therefore, when the following rows are executedCodeThe javascript interpreter only allocates memory to the elements whose subscript is 0 and 10000, instead of allocating memory to the 10000 elements whose subscript is between 0 and 9999.

 
A [0] = 1; A [10000] = "this is element 10000 ";

② Delete array elements

The delete operator sets an array element to the undefined value, but the element itself continues to exist. To delete an element, you must use the array method.

Array. Shift ()Method to delete the first element of the array.

Array. Pop ()Method to delete the last element.

Array. splice ()Method to delete an element in a continuous range from an array.

③ Length of the array

All arrays have a special attribute length to indicate the number of elements contained in the array.

④ Traverse the Array

The most common use of the Length attribute of an array is to traverse array elements.

 
VaR fruits = ["Mango", "banana", "Cherry", "Pear"]; for (VAR I = 0; I <fruits. length; I ++) {alert (fruits [I]);}

⑤ Truncate or increase the Array

The Length attribute of the array can be read or written. If length is set to a value smaller than its current value, the array will be truncated, elements other than the length will be discarded, and their values will be lost.

If the value set for length is greater than the current value, the new and undefined elements will be added to the end of the array to increase the array to the new length.

⑥ Multi-dimensional array

Although JavaScript does not support real multi-dimensional arrays, it allows elements to be arrays, which is very close to multi-dimensional 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.