数组的定义
/* 数组定义方法1 */
var name_1 = new Array(a,b,c,..)
/* 数组定义方法2 */
var name_2 = [m,n,t,...]
案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
/* 数组定义方法1 */
var aa = new Array(1,2,3)
/* 数组定义方法2 */
var bb = [11,22,33]
console.log(aa);
console.log(bb);
</script>
</body>
</html>
特点:长度和类型都可变
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var aa = new Array(1,2,3)
/* 数组长度是可变的 */
aa[10] = 10;
console.log(aa);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var aa = new Array(1,2,3)
/* 数组类型是可变的 */
aa[6]="A";
aa[8]=true;
console.log(aa);
</script>
</body>
</html>
属性和方法
arr.length;
arr.forEach(function(name){ 运行代码});
arr.forEach((name)=>{运行代码});
arr.push(1,"字符串");
arr.splice(下标,所删减的数量);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var arr = [0,1,2,3,4]
/* 获取数组长度 */
console.log(arr.length);
console.log("---------------------------------");
/* 遍历数组有值元素方法1 */
arr.forEach(function(e){
console.log(e);
})
console.log("---------------------------------");
/* 遍历数组有值元素方法1 */
arr.forEach((r) => {
console.log(r);
})
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var arr = [0,1,2,3,4]
/* 添加元素 */
arr.push(7,8,9,'a','d');
console.log(arr);
console.log("---------------------------------");
/* 删除元素 */
arr.splice(0,3)
console.log(arr);
console.log("---------------------------------");
</script>
</body>
</html>