Home > Web Front-end > JS Tutorial > body text

Take you to know the array definition and multi-dimensional array in JavaScript

伊谢尔伦
Release: 2017-07-25 11:45:17
Original
3837 people have browsed it

Understanding arrays

An array is a collection of certain types of data. The data type can be an integer, a string, or even an object.
Javascript does not support multi-dimensional arrays, but because arrays can contain objects (an array is also an object), arrays can achieve functions similar to multi-dimensional arrays by nesting each other.

Define array

Declare an array with 10 elements:

var a = new Array(10);
Copy after login

At this time, the memory space has been opened for a, containing 10 elements. Use the array name plus [subscript] to call, such as a[2], but the element is not initialized at this time, and the call will return undefined.
The following code defines a variable array and assigns it.

var a = new Array();
a[0] = 10;
a[1] = "aaa";
a[2] = 12.6;
Copy after login

As mentioned above, objects can be placed in arrays, such as the following code:

var a =    new Array();
a[0]    = true;
a[1]    = document.getElementById("text");
a[2]    = {x:11, y:22};
a[3]    = new Array();
Copy after login

Arrays can also be assigned values ​​directly when instantiated, for example:

var a = new Array(1, 2, 3, 4, 5);
var b = [1, 2, 3, 4, 5];
Copy after login

a and b is both an array, but b uses an implicit declaration to create another instance. If alert(a==b) is used at this time, false will pop up

Multidimensional array

In fact, Javascript does not support multi-dimensional arrays. In asp, you can use dim a(10,3) to define multi-dimensional arrays. In Javascript, if you use var a = new Array(10,3), an error will be reported
But as mentioned before, arrays can contain objects, so you can declare an element in the array as an array, for example:

var a = new Array();
a[0] = new Array();
a[0][0] = 1;
alert(a[0][0]);  //弹出 1
Copy after login

Assign a value when declaring

var a = new Array([1,2,3], [4,5,6],[7,8,9]);
var b = [[1,2,3], [4,5,6], [7,8,9]];
Copy after login

The effect is the same, a Using conventional instantiation, b is an implicit declaration, and the result is a multi-dimensional array.

Array literals

I really don’t know what this is called in Chinese, literal array?
Speaking of arrays, we have to talk about Array Literals. Arrays are actually special objects. Objects have unique properties and methods. Values ​​and calls are obtained through object name.property, object.method(), and arrays are obtained through the following Markers are used to get values. Array Literals are similar to arrays in many ways. They are both collections of a certain data type. However, Array Literals is fundamentally an object, and its declaration and call are different from arrays:

var aa = new Object();
aa.x = "cat";
aa.y = "sunny";
alert(aa.x);    //弹出cat
Copy after login

Creation A simple object, generally called through aa. , the result is the same

The above is the detailed content of Take you to know the array definition and multi-dimensional array in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!