Array array obj...LOGIN

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 = [<Element 1>, <Element 2>, <Element 3>. ..];

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: <array object>.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

<html>
<body>
<script type="text/javascript">
var mycars = new Array()
mycars[0] = "Saab"
mycars[1] = "Volvo"
mycars[2] = "BMW"
for (i=0;i<mycars.length;i++)
{
document.write(mycars[i] + "<br />")
}
</script>
</body>
</html>

Merge two arrays - concat()

<html>
<body>
<script type="text/javascript">
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
var arr2 = new Array(3)
arr2[0] = "James"
arr2[1] = "Adrew"
arr2[2] = "Martin"
document.write(arr.concat(arr2))
</script>
</body>
</html>


Next Section
<!DOCTYPE html> <html> <head> <title> 事件</title> <script type="text/javascript"> var myArray1 = [2, 8, 6]; var myArray2 = [1, 2, 3]; document.write(myArray1.concat(myArray2)); </script> </head> <body> </body> </html>
submitReset Code
ChapterCourseware