<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>数组出栈入栈</title>
</head>
<body>
<script>
let hxj=["haoxuejie",20];
//数组出栈arr.pop() 与其对应的是入栈push();
console.log(hxj.pop());//20 返回弹出的元素
console.log(hxj);//["haoxuejie"]
//push()是从数组尾部加,unshift()是从数组头部加
let ydc=["ydc",28];
console.log(ydc.unshift("male"));//3,返回数组个数 与push一样都是返回新数组个数
console.log(ydc);// ["male", "ydc", 28];
//从数组头部移除
console.log(ydc.shift());//male,返回移除的元素,与pop类似
console.log(ydc);//["ydc", 28]
//填充数组
console.log(Array(5).fill("hxj"));//["hxj", "hxj", "hxj", "hxj", "hxj"]
console.log([1,2,3,4,5].fill("hxj",2,4));//填充的索引值为[2,4)
</script>
</body>
</html>
网友评论