How to use JavaScript Arrays

Source: Internet
Author: User
In JavaScript, you can use newArray to create an array. The following three syntaxes are correct: arrayObjnewArray () creates an array, arrayObjnewArray ([size]), creates an array, and specifies the length, note that it is not the upper limit. It is the length of arrayObjnewArray ([element0 [, element1 [,... [, ele

In JavaScript, you can use new Array to create an Array. The following three syntaxes are correct:

ArrayObj = new Array () to create an Array

ArrayObj = new Array ([size]) Create an Array and specify the length. Note that the length is not the upper limit.

ArrayObj = new Array ([element0 [, element1 [,... [, elementN]) Create an Array and assign values

It should be noted that, although the second method creates an array with a specified length, the array actually gets longer in all cases, that is, even if the length is 5, you can still store elements outside the specified length. Note: The length will change accordingly.

--------------------------------------------------------------------------------
Does new Array (5) Create an Array with a length of 5 or an Array with an element value of 5? Create an array of 5 characters.

Is the array subscript starting from 0 or starting from 1? Starting from 0, so the upper limit of the array is equal to the length of the array-1.

What is the maximum value of the underlying mark of the array? The power of 2 is reduced by 2 to the power of 32, that is, 4294967295, about 4 billion. That's enough.

Will the array be automatically rounded up when the subscript is decimal? No, it will ignore or cause a runtime error.

Is multidimensional array supported? Not Supported! However, each element of the array can be further defined as an array to achieve the goal of multi-dimensional arrays.

How to access array elements? Use "[]". For example, if the array is named arr and you want to access the first element, use arr [0].

--------------------------------------------------------------------------------
JavaScript Array (JScript version 2) has three attributes and 13 methods. Only length is important among the three attributes, but it is relatively simple. In addition, constructor and prototype attributes are common and not commonly used, so we will not introduce the Array attribute, the introduction of 13 method groups in Array makes it easier to remember.
Pop and push: pop removes the last element and returns the value of this element. push ([item1 [item2 [... [itemN]) adds one or more new elements to the end of the array, and returns the new length of the array, if an array is added, use a comma to connect the elements of the array and then add it.
Shift and unshift: respectively correspond to pop and push, but this is done at the beginning of the array. Note that when elements are removed or added from the starting position, the elements in the array are moved forward or backward.
Slice and splice: slice (start, [end]) returns a part of the array in the form of an array. Note that the end element is not included. If the end is omitted, all the elements after the start are copied; splice (start, deleteCount, [item1 [, item2 [,... [, itemN]) removes one or more elements from the array. If necessary, insert new elements to the position of the removed elements. Return the removed elements in the array format, if an array is inserted, only the first element of the array is inserted.
Reverse and sort: reverse () reverses the elements (first to last and last to the first) and returns the array address; sort () sorts the array and returns the array address.
Concat and join: concat concatenates multiple arrays (or strings) into an array. join returns a string that concatenates each element value of the array, separated by separator.
ToLocaleString, toString, valueOf: can be considered as a special use of join, not commonly used.
--------------------------------------------------------------------------------

Delete array elements

I once saw an article saying that the steps for deleting an element in the array should be as follows:
1. delete
2. element forward
3. Reset the array length

In fact, using the array method splice can be completed in one step.
ArrayObject. splice (start, deleteCount, [item1 [, item2 [,... [, itemN])

Start location to be deleted

DeleteCount

If item1, item2,..., itemN specifies this value, you can insert an element to the deleted position. If an array is inserted, only the first element is taken.


--------------------------------------------------------------------------------


Copy an array

Sometimes you need to assign the value of one array to another and use the following code:

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.