数组添加元素
2020-03-16 本文已影响0人
小雪洁
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>数组添加元素</title>
</head>
<body>
<script>
//方法1:
let array =["hxj","ydc"];
array[array.length]="30";
array[array.length]="28";
console.log(array);
//方法2:使用点语法
let a=["hxj",30];
let b=["ydc",20];
let ab=[...a,...b];
console.log(ab);//["hxj", 30, "ydc", 20]
console.log(...a,...b);//hxj 30 ydc 20
let c=["hxj","30"];
let d=["ydc","20"];
//注意c.push()直接改变c的元素,返回值是新数组的个数
console.log(c.push(...d));//4
console.log(c);//["hxj", "30", "ydc", "20"]
c.push("hhh",...d);
console.log(c);
</script>
</body>
</html>