70 lines
1.5 KiB
HTML
70 lines
1.5 KiB
HTML
<!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>
|
|
<script>
|
|
var arr=[3,9,6,4,5]
|
|
var arr1=[1,'a','p']
|
|
console.log(arr1)
|
|
console.log(arr1[2])
|
|
// 访问数组的时候需要加上数组下标
|
|
// 数组下标从0开始
|
|
|
|
|
|
// 数组遍历 (用循环输出数组的每一项)
|
|
// for循环
|
|
// for in
|
|
// for(let i=0;i<arr.length;i++){
|
|
// console.log(arr[i])
|
|
// }
|
|
|
|
// for(let i in arr1){
|
|
// console.log(arr1[i],i)
|
|
// }
|
|
|
|
// 数组的操作
|
|
// 增删改查
|
|
// 增
|
|
var arr2=[1,2,3,4]
|
|
arr2.push('z') //添加一个元素
|
|
// arr2[4]="m"
|
|
arr2.unshift("qq")
|
|
// console.log(arr2[4])
|
|
console.log(arr2,"1234")
|
|
arr2.splice(4,0,'m')
|
|
|
|
|
|
|
|
// 修改
|
|
arr2[3]=5
|
|
|
|
arr2.splice(2,1,6)
|
|
|
|
|
|
// 删除
|
|
arr2.splice(3,1)
|
|
console.log(arr.pop(),"zaza")
|
|
console.log(arr.shift(),"zaza")
|
|
console.log(arr2)
|
|
|
|
// 排序
|
|
arr.sort()
|
|
console.log(arr)
|
|
arr.reverse()
|
|
console.log(arr)
|
|
|
|
// 转换
|
|
var str=arr.toString()
|
|
// join(',')
|
|
console.log(str)
|
|
console.log(str.split(','))
|
|
|
|
|
|
</script>
|
|
<body>
|
|
|
|
</body>
|
|
</html> |