文章目录
数组的栈方法和队列方法
<!DOCTYPE html>
<html lang="UTF-8">
<head>
<meta charset="UTF-8">
<title>数组常用的方法</title>
</head>
<body>
<script type="text/javascript">
//Array(数组)的定义
//let colors = new Array('red', 'blue', 'green');
let colors = ['red', 'blue', 'green'];
//使用'|'分割字符串然后合并成一个String给a
let a = colors.join('|');
console.log(a);
//栈lifo,先进后出 push() pop队列方法
//push()往数组的末尾加数据
let newlength = colors.push('orange');
console.log('The length is '+newlength);
console.log(colors);
//pop()把数组的末尾弹出
let lastItem = colors.pop();
console.log(lastItem);
console.log(colors);
//队列,先进先出 shift() 和 unshift()
//unshift()放进队列的头
newlength = colors.unshift('yellow');
console.log(newlength);
console.log(colors);
//shift()把队列的头取出,yellow
//let firstItem = colors.shift();
console.log(firstItem);
console.log(colors);
</script>
</body>
</html>