当前位置:网站首页>JS创建一个数组(字面量)
JS创建一个数组(字面量)
2022-06-23 14:24:00 【刘家奕_】
(1)使用字面量来创建数组//语法:[]
var arr = [];
(2)使用字面量创建数组时,可以在创建时就指定数组中的元素
var arr = [1,2,3,4,5,10];
(3)使用构造函数创建数组时,也可以同时添加元素,将要添加的元素作文构造函数的参数传递 元素之间使用,隔开
var arr2 =new Array( 10,20,30);
console.log( arr2);
(4)创建一个数组数组中只有一个元素10
arr = [ 10];
(5)创建一个长度为10的数组
arr2 = new Array ( 10);
(6)数组中的元素可以是任意的数据类型
arr = [ "hello",1,true,nul1,undefined];
也可以是对象
arr= [{name : "孙悟空"},{name : "沙和尚"},{name : "猪八戒"}];
<!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: "孙悟空" }, { name: "沙和尚" }, { name: "猪八戒" }];
alert(arr[1].name);
</script>
</body>
</html>

(7)也可以是一个函数
arr = [function(){alert(1)},function(){alert(2)}];
console.log( arr);
arr[0]();

(8) 数组中也可以放数组,如下这种数组我们称为二维数组
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>

边栏推荐
猜你喜欢
Redis缓存三大异常的处理方案梳理总结
![[Level 2 warranty] which brand of Fortress machine is good for Level 2 warranty?](/img/84/4fa07c54786aaf423ca333ecde62f4.png)
[Level 2 warranty] which brand of Fortress machine is good for Level 2 warranty?

SQL injection vulnerability (principle)

useState vs useRef 和 useReducer:相同点、不同点和用例

用OBS做直播推流简易教程
Xampp中mysql无法启动问题的解决方法

How to use note taking software flowus and note for interval repetition? Based on formula template

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

加快 yarn install 的三个简单技巧

Effect evaluation of regression model under credit product quota pricing scenario
随机推荐
How is it safe to open an account for futures? Which futures company has a relatively low handling fee for futures and is suitable for retail investors to open an account?
useState vs useRef 和 useReducer:相同点、不同点和用例
What do you mean by waiting for insurance records? Where should I go for filing?
Mysql数据库---日志管理、备份与恢复
How can genetic testing help patients fight disease?
2021-05-22
The second Tencent light · public welfare innovation challenge was launched, and the three competition topics focused on the social value of sustainable development
杀入美团、饿了么腹地,京东外卖劲儿有多大?
Test article
Error creating bean with name xxx Factory method ‘sqlSessionFactory‘ threw exception; nested excepti
物流贸易相关
vim备份历史命令
Introduction to helm basics helm introduction and installation
Uniswap 收购 NFT交易聚合器 Genie,NFT 交易市场将生变局?
The team of China University of Mines developed an integrated multi-scale deep learning model for RNA methylation site prediction
【云驻共创】制造业企业如何建设“条码工厂”
阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题
ACM Player Illustration leetcode remove element
2021-05-08
ai智能机器人让我们工作省时省力