JavaScript_ notes 9 Array type

Features of Array type

Each item in the ECMAScript array can hold any type of data.
For example: the first position of the array can store strings, the second position can store values, and the third position can store objects. That is, the data type stored in each item in the same array is not restricted.

The way to create an Array

There are two basic ways to create an array. One is to use the Array constructor ; the other is the array literal notation.

Method one, use the Array constructor

To use the Array constructor to create an array, you need to use new and the constructor Array().
Code such as:

var people = new Array();

The above Array constructor does not take parameters;
while the Array constructor can receive parameters, the situation of a parameter is as follows:

  • If the value is passed, an array with the corresponding number of items will be created;
  • If other types of parameters are passed, an array with only one item and the value will be created.

Code such as:

var people = new Array(10);//创建了包含有10项的数组;
var people = new Array("Kabukiyo"); //创建了只有一项且保存值为“Kabukiyo”的数组

Multiple parameters

Code such as:

var people = new Array("metal","core","djent","kabukiyo");//创建了包含四个字符串值的数组。

Using the Array constructor, the new operator can be omitted , and the result is the same.
Code such as:

var people = Array();
var people = Array(10);
var people = Array("kabukiyo");
var people = Array("metal","core","djent","kabukiyo");

Method two, array literal representation

Using array literal notation, you can use the following code as an example:

var people = ["Lin","Zhang","Huang"];
var people = [];

The first line creates an array of three strings; the second line creates an empty array.
The following wording is not recommended, because it may be different in different versions:

var people = [1,2,]; //可能会包含是2项或3项的数组
var people = [,,,,,]; //可能会包含是5项或6项的数组

Add, delete, check and modify array

Square brackets access array && increase check and change

var people = ["Lin","Zhang","Huang"];
alert("people[0]");    //显示第一项 "查"
people[1] = "Liu";     //修改第二项 "改"
people[3] = "He";      //增加第四项 "增"
alert(people.length);; // 4 数组长度

The length of the array is modifiable

The length of the array is not only readable, it is modifiable.
By modifying the length of the array, we can add or delete items to the array.
Increase the length of the array, and each new item will be undefined;

var people = ["Lin","Zhang","Huang"];
alert(people.length);      // 3
people.length = 5;         // 让长度变成4,新增了2项
alert("people[3]");      //新增的每一项都是 undefined; 
alert("people[4]");      //新增的每一项都是 undefined; 

Make the length of the array smaller, and access to the deleted item will be undefined;

var people = ["Lin","Zhang","Huang"];
alert(people.length);      // 3
people.length=2;
alert("people[2]")        //undefined;

Array detection method

For a web page or a global scope, use the instanceof operator:

if(people instanceof Array){
    
    
        //
}

Another method, Array.isArray():

if (Array.isArray(people)){
    
    
  		//
}

Array conversion method

Three-level directory

Guess you like

Origin blog.csdn.net/qq_43263320/article/details/113809923
Recommended