当前位置:网站首页>JS create an array (literal)
JS create an array (literal)
2022-06-23 15:11:00 【Liujiayi_】
(1) Use literals to create arrays // grammar :[]
var arr = [];
(2) When you create an array using literals , You can specify the elements in the array at creation time
var arr = [1,2,3,4,5,10];
(3) When creating an array using a constructor , You can also add elements at the same time , The element to be added is passed as a parameter of the constructor Use... Between elements , separate
var arr2 =new Array( 10,20,30);
console.log( arr2);
(4) Create an array with only one element 10
arr = [ 10];
(5) Create a length of 10 Array of
arr2 = new Array ( 10);
(6) The elements in the array can be any data type
arr = [ "hello",1,true,nul1,undefined];
It could be an object
arr= [{name : " The Monkey King "},{name : " The sand monk "},{name : " Pig eight quit "}];
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
arr = [{ name: " The Monkey King " }, { name: " The sand monk " }, { name: " Pig eight quit " }];
alert(arr[1].name);
</script>
</body>
</html>

(7) It can also be a function
arr = [function(){alert(1)},function(){alert(2)}];
console.log( arr);
arr[0]();

(8) You can also put an array in the array , The following array is called two-dimensional array
arr = [[1,2,3],[3,4,5],[5,6,7]];
console.log( arr[1]);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
arr = [
[1, 2, 3],
[3, 4, 5],
[5, 6, 7],
];
alert(arr[1]);
</script>
</body>
</html>

边栏推荐
- Un million de bonus vous attend, le premier concours d'innovation et d'application de la Chine Yuan cosmique Joint Venture Black Horse Hot Recruitment!
- 2021-06-03
- WebService interface publishing and calling
- 阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题
- Summary of operating system underlying knowledge (interview)
- 力扣解法匯總513-找樹左下角的值
- ACM Player Illustration leetcode remove element
- 2021-05-08
- [Level 2 warranty] which brand of Fortress machine is good for Level 2 warranty?
- Sectigo(Comodo)证书的由来
猜你喜欢

Selenium Edge的IE模式

Summary of operating system underlying knowledge (interview)

腾讯云服务器发送邮件失败

Introduction to helm basics helm introduction and installation

如何解决 Iterative 半监督训练 在 ASR 训练中难以落地的问题丨RTC Dev Meetup

基因检测,如何帮助患者对抗疾病?

图解OneFlow的学习率调整策略

JS创建一个数组(字面量)
详解Redis分布式锁的原理与实现

Self inspection is recommended! The transaction caused by MySQL driver bug is not rolled back. Maybe you are facing this risk!
随机推荐
Analysis and solution of connection failure caused by MySQL using replicationconnection
2021-05-08
力扣解法匯總513-找樹左下角的值
JSR303数据校验
js遍历数组(用forEach()方法)
MySQL create and manage tables
物流贸易相关
Auto - vérification recommandée! Les bogues MySQL ne font pas reculer les transactions, peut - être êtes - vous à risque!
2021-05-08
[cloud based co creation] how manufacturing enterprises build "barcode factories"
打印内存站信息
Résumé de la méthode de déduction de la force 513 - trouver la valeur du coin inférieur gauche de l'arbre
Gartner最新报告:低代码应用开发平台在国内的发展
2021-05-22
系统设计与分析-技术报告-定时清理验证码的一种解决方案
KDD'22「阿里」推荐系统中的通用序列表征学习
Idea view View the class file idea Class folder
[pyside2] pyside2 window is on the top of Maya (note)
建议自查!MySQL驱动Bug引发的事务不回滚问题,也许你正面临该风险!
General sequence representation learning in kdd'22 "Ali" recommendation system