js A simple way to create an array

  • 2021-07-06 09:39:52
  • OfStack

1. Declaring methods for arrays


 ( 1 ):  arrayObj = new Array(); // Create 1 An array of. 

The code is as follows:


var arr1 = new Array();

 ( 2 ): arrayObj = new Array([size])   
 Create 1 Array and specify the length, note that it is not the upper limit, but the length. 

The code is as follows:


var a = new Array(5);

 ( 3 ): arrayObj = new Array([element0[, element1[, ...[, elementN]]]])   
 Create 1 Array and assign values. 

The code is as follows:


var a = new Array(["b", 2, "a", 4,]);

 ( 4 ): arrayObj = [element0, element1, ..., elementN]   
 Create 1 Short for arrays and assigning values. Note that parentheses here do not mean that they can be omitted. 

The code is as follows:


var a = ["b", 2, "a", 4,];

Note: Pay attention to the difference between taking "[]" and not taking "[]"

The code is as follows:


var a = new Array(5); // Refers to the creation length is 5 Array of 
var a = new Array([5]); // Refers to creating 1 An array of arrays with a length of 1 , and the first 1 Bit is 5

2. Common methods of arrays

3. Array operation (pass address)

The code is as follows:


var t2=new Array();
t2[0]=1;
t2[1]=2;
test2(t2); // Pass address (array) 

function test2(var2) {
for(var i=0;i
var2[i]=var2[i]+1;
}
}
for(var i=0;i
alert(t2[i]);
}

Related articles: