数组除了定义不需要数据类型以外,还多了一个高级运算符“...”,使用方法如下:
<!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>
//定义数组
let arr = [10, 20, 30];
arr[3] = 40;//可以直接添加元素
for(let i = 0; i < arr.length; i ++){
document.write(arr[i] + "<br>");
}
//数组的高级运算符 ...
//复制数组
let arr2 = [...arr];
for(let i = 0; i < arr2.length; i ++){
document.write(arr2[i] + "<br>");
}
document.write("===================<br>");
//合并数组
let arr3 = [40, 50, 60];
let arr4 = arr3.concat(arr);
document.write(arr3.concat(arr) + "<br>");
for(let i = 0; i < arr4.length; i ++){
document.write(arr4[i] + "<br>");
}
document.write("===================<br>");
//将字符串转换成数组
let arr5 = [..."hello"];
for(let i = 0; i < arr5.length; i ++){
document.write(arr5[i] + "<br>");
}
</script>
</body>
</html>