如何使用常量值初始化JavaScript数组?

时间:2022-06-12 17:07:27

How do you initialize a JavaScript array with constant values? For example, in C code I can write

如何使用常量值初始化JavaScript数组?例如,在C代码中我可以写

int array[] = {1, 2, 3};

What is the equivalent in JavaScript?

JavaScript中的等价物是什么?

4 个解决方案

#1


7  

1: Regular:

1:常规:

 var myCars=new Array(); 
 myCars[0]="Saab";       
 myCars[1]="Volvo";
 myCars[2]="BMW";

2: Condensed:

2:凝聚态:

 var myCars=new Array("Saab","Volvo","BMW");

3: Literal:

3:文字:

 var myCars=["Saab","Volvo","BMW"];

Refer this link: https://www.w3schools.com/jsref/jsref_obj_array.asp

请参考此链接:https://www.w3schools.com/jsref/jsref_obj_array.asp

#2


1  

In the following way:

通过以下方式:

var array = [1, 2, 3];

Read more about arrays in MDN:

阅读有关MDN中数组的更多信息:

#3


1  

try this:

尝试这个:

var array = [1,2,3];

#4


0  

Array object in javascript :

javascript中的数组对象:

var mycars = new Array();
mycars[0] = "1";
mycars[1] = "2";
mycars[2] = "3";

or

要么

var myCars=["one","two","three"];

#1


7  

1: Regular:

1:常规:

 var myCars=new Array(); 
 myCars[0]="Saab";       
 myCars[1]="Volvo";
 myCars[2]="BMW";

2: Condensed:

2:凝聚态:

 var myCars=new Array("Saab","Volvo","BMW");

3: Literal:

3:文字:

 var myCars=["Saab","Volvo","BMW"];

Refer this link: https://www.w3schools.com/jsref/jsref_obj_array.asp

请参考此链接:https://www.w3schools.com/jsref/jsref_obj_array.asp

#2


1  

In the following way:

通过以下方式:

var array = [1, 2, 3];

Read more about arrays in MDN:

阅读有关MDN中数组的更多信息:

#3


1  

try this:

尝试这个:

var array = [1,2,3];

#4


0  

Array object in javascript :

javascript中的数组对象:

var mycars = new Array();
mycars[0] = "1";
mycars[1] = "2";
mycars[2] = "3";

or

要么

var myCars=["one","two","three"];