Array array object

Array array object

An array object is a collection of objects, and the objects inside can be of different types. Each member object of the array has a "subscript" that represents its position in the array (counting starts from 0).

The array subscript is expressed by using square brackets, for example:

myArray[2]="hello"

Note: JavaScript only has one-dimensional arrays. To use multi-dimensional arrays, please use this virtual method:

var myArray = new Array(new Array(), new Array(), new Array(), ...);

In fact, this is a one-dimensional array, and each element in it is an array. When calling the elements of this "two-dimensional array":

myArray[2][3] = ...;

(1)Array's property

length: Returns the length of the array, that is How many elements are there in the array. It is equal to the index of the last element in the array plus one.

Therefore, if you want to add an element, you only need:

`` myArray[myArray.length] = ...; ```

Method of array definition:

1. An empty array is defined:

var Array name = new Array();

2. Specify an array with n empty elements when defining:

var Array name = new Array(n) ;

3. When defining an array, initialize the data directly:

var array name = [, , . ..];

We define the myArray array and assign it a value. The code is as follows:

var myArray = [2, 8, 6];

Description: An array myArray is defined, the elements inside are: myArray[0] = 2; myArray[1] = 8; myArray[2] = 6.

Use of array elements:

Array name [subscript] = value;

Note: The subscript of the array is enclosed in square brackets Get up and start from 0.

Array properties:

length Usage: .length; Returns: the length of the array, that is, how many elements there are in the array. It is equal to the index of the last element in the array plus one.

(2)Methods of Array

join("Specify separator"): Return a string to string the array elements together. Elements are separated by the specified delimiter.

toString(): Convert the array to a string and return the result.

reverse(): Reverse the order of array elements.

slice(n,m): Returns a subarray, from the nth element to the mth element of the array.

sort(SortFunction): Sort the elements of the array according to the specified SortFunction.

concat(Array_1,Array_2): Used to connect two or more arrays.

Create array

    

Merge two arrays - concat()

    


Continuing Learning
||
事件
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!