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

Detailed explanation of array usage in JS

php中世界最好的语言
Release: 2018-04-13 10:30:22
Original
1106 people have browsed it

This time I will bring you a detailed explanation of array usage in JS, and what are the notes when using JS arrays. Here is a practical case, let’s take a look.

Many people who have just started to know js are not very clear about what js array is and how js array is defined. The following article will introduce to you the definition of js array. Those who are interested in this are as follows: Get up and find out.

1. What is an array

An array is a collection of data

Its form of expression is a continuous memory address in the memory

The array name is actually the first address of the consecutive memory addresses

2. About the characteristics of arrays in js

There is no need to specify the data type when defining the array

There is no need to specify the array length when defining the array

Arrays can store data of any data type (for example, one element stores integer type, and one element stores string type. This is possible in JS)

Syntax for creating an array:

var arr=[value 1, value 2, value 3];                                                                                                                         var arr=new Array(value 1, value 2, value 3); //Direct instantiation

var array=new Array(size); //Create an array and specify the length

Meaning of symbols in JS:

() indicates function execution

[] represents syntax simulation, which represents simulating an instance of the Array class (=new Array())

{} represents syntax simulation, which represents the simulation of instances of

Object class

(=new Object()) //Indicates syntax simulation (regular object), which means simulating instances of the RegExp class (=new RegExp())

//方法一 
 var arr1 = [1,3,5,7,9]; 
 document.write(arr1[2] + '<br>');
//方法二 
var arr2 = new Array(2,4,6,8,10); 
document.write(arr2[3] + '<br>'); 
//方法三 
var arr3 = new Array(3);//固定数组长度为3 
arr3[0] = 1; 
arr3[1] = 2; 
arr3[2] = 3; 
document.write(arr3[2] + '<br>');
Copy after login

3. About array length

Array object.length

In js, each array object can call the length attribute, which indicates how many array elements there are under the array object

Example:

var row = ['zhangsan','lisi','wangwu']; 
 doucument.write('共有'+row.length+'个人<br>'); 
 var length = row.length;//对数组进行遍历 
for (var i=0;i<length;i++){ 
 doucument.write(row[i]+'<br>'); }
Copy after login

4. for...in statement

In js, arrays are not data types. The data type of arrays is actually objects

The For.....in statement in Js can traverse all properties of an object

You can also use the for...in statement to traverse all elements of an array

grammar:

for(var i in array){

}

Principle: There are several elements in the array, and the for..in statement will be executed in a loop as many times as possible

Each time it is executed, the subscript of the current array element is stored in variable i

var row = ['zhangsan','lisi','wangwu','xiaoqiang']; 
for (var i in row){
document.write(i + ':' + row[i] + '<br>'); 
}
Copy after login

result:

0:zhangsa

1:lisi

2:wangwu

3:xiaoqian

5. Text subscript

Format:

arr['key'] = value;

In js, array elements with text subscripts are not included in the length of the array

Added to the array in the form of a text subscript, but actually added to the array object in the form of an attribute

var arr = [1,2,3]; 
arr['first'] = 'zhangsan';
arr['second'] = 'lisi'; 
document.write(arr.length + '<br>');
document.write(arr.first + '<br>');
document.write(arr.second + '<br>');
Copy after login

result:

3

zhangsan

lisi

Iterate over an array with text subscripts:

var arr = [1,2,3];
arr['first'] = 'zhangsan';
arr['second'] = 'lisi'; 
for(var i in arr){
document.write(i + ':' + arr[i] + '<br>'); 
}
Copy after login

result:

0:1

1:2

2:3

first:zhangsan

second:lisi

6.

Multidimensional array

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

JS prompt text box email address completion

getBoundingClientRect usage and compatibility handling

The above is the detailed content of Detailed explanation of array usage in JS. 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