Introduction to JavaScript Array object usage

Source: Internet
Author: User
Tags array definition arrays javascript array in python

Array definition
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:

The code is as follows Copy Code

var mycars=new Array ()
mycars[0]= "Saab"
mycars[1]= "Volvo"
mycars[2]= "BMW" can also use an integer argument to control the size of the array:

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

1, the creation of the array

The code is as follows Copy Code

var arrayobj = new Array (); Create an array

var arrayobj = new Array ([size]); Create an array and specify the length, note not the upper limit, is the length

var arrayobj = new Array ([element0[, element1[, ...) [, ELEMENTN]]]); Create an array and assign a value

To illustrate, although the second method creates an array that specifies the length, in all cases the array is longer, that is, even if you specify a length of 5, you can still store the elements outside the specified length, note: the length changes.

2, access to elements of the array

The code is as follows Copy Code

var testgetarrvalue=arrayobj[1]; Gets the element value of an array

Arrayobj[1]= "This is the new value"; Give a new value to an array element

3, the addition of array elements

The code is as follows Copy Code

Code
Arrayobj. Push ([Item1 [item2 [...] [Itemn]]]); /Adds one or more new elements to the end of the array and returns the new length of the array

Arrayobj.unshift ([Item1 [item2 [...] [Itemn]]]); /Adds one or more new elements to the beginning of the array, the elements in the array are automatically moved back, and the new length of the array is returned

Arrayobj.splice (insertpos,0,[item1[, item2[, ...) [, Itemn]]]); /inserts one or more new elements into the array at the specified position, and the element at the insertion point is automatically moved back to "".

4, the deletion of the elements of the array

The code is as follows Copy Code

Arrayobj.pop (); Removes the last element and returns the element value

Arrayobj.shift (); Removes the first element and returns the element value, and the elements in the array are automatically moved forward

Arrayobj.splice (Deletepos,deletecount); Deletes the specified number of DeleteCount elements starting at the specified position, deletepos the removed elements

5, the array of interception and merging

Arrayobj.slice (start, [end]); Returns a portion of an array as an array, noting that the end-corresponding element is not included, and if omitting the end copies all elements after start

Arrayobj.concat ([item1[, item2[, ...) [, Itemn]]]); Concatenate multiple arrays (or a string, or a mixture of arrays and strings) into an array, returning a new array of connections

6, the copy of the array

Arrayobj.slice (0); Returns an array of copies, noting that a new array is not a pointer to the

Arrayobj.concat (); Returns an array of copies, noting that a new array is not a pointer to the

7, the ordering of array elements

Arrayobj.reverse (); Reverse element (top to last, last to top), return array address

Arrayobj.sort (); array element Sorting, returning the arrays address

8. String of array elements

Arrayobj.join (separator); Returns a string that connects each element value of an array, separated by a separator in the middle.

toLocaleString, toString, valueof: Can be seen as a special use of join, not commonly used

From the subscript of an array into an indexed array, an associative array

  code is as follows copy code

/* Indexed array, That is, the usual case of the array */
var ary1 = [1,3,5,8];
//By index to take array elements, starting from 0 (of course, some language implementations starting from 1), the index is actually ordinal, an integer number
Ary1[0];
Ary1[1];
Ary1[2];
Ary1[3];

The

/* associative array, which is an array of non ordinal types to be accessed in Python called dictionary/
var ary2 = {};//accessed by non-ordinal (number), here is the string
ary2["one" = 1;
ary2["two"] = 2;
ary2["THR"] = 3;
ary2["fou"] = 4;

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.