1 <script>
2 /*
3 JavaSrcipt数组与其他语言中的数组的区别
4 同:有序列表
5 不同:数组的每一项可以保存任何类型的数据,数组的大小可以动态调整
6
7 创建数组的方式:
8 1.使用Array构造函数
9 */
10 var color= new Array()
11 var color= new Array(20)//创建一个包含20个项的数组
12 var color= new Array('red', 'blue', 'green')//创建一个包含3个字符串值的数组
13 var color= Array(20)//省略new操作符创建一个包含20个项的数组
14
15 /*
16 2.使用数组字面量表示法
17 数组字面量由一对包含数组项的方括号表示,多个数组项之间用逗号隔开
18 */
19 var color= ['red', 'blue', 'green']//创建一个包含3个字符串值的数组
20 var names= []//创建一个空数组
21 var values= [2,3,]//会创建一个包含2或3项的数组,建议不要这样使用
22 var options= [,,,,,]//会创建一个包含5或6项的数组,建议不要这样使用
23
24
25 /*
26 读取和设置数组的值
27 使用方括号并提供相应值的基于0的数字索引
28 方扩号中的所有表示要访问的值,如果索引值小于数组中的项数,则返回对应的值,如果索引值超过了数组现有最大索引值,数组就会自动增加到最大索引值加1的长度
29 */
30 var color= ['red', 'blue', 'green']
31 console.log(color[0])//显示第一项 red
32 color[2]= 'black'//修改第三项 ["red", "blue", "black"]
33 console.log(color)
34 color[3]= 'brown'//新增第四项 ["red", "blue", "black", "brown"]
35 console.log(color)
36
37 /*
38 length属性
39 始终会返回0或更大的值
40 */
41 var colors= ['red', 'blue', 'green']
42 console.log(colors.length) //3
43 //数组的length不是只读的,还可以从数组的末尾移除项或者向数组中添加新项
44 colors.length= 2
45 console.log(colors)// 末尾移除1项 ["red", "blue"]
46 colors.length= 4
47 console.log(colors.length)// 4 向数组中添加新项,新增的每一项都会取得undefined值
48 //利用length在末尾添加新项
49 var colors= ['red', 'blue', 'green']
50 colors[colors.length]= 'black'//在3的位置添加
51 colors[colors.length]= 'brown'//在4的位置添加
52 console.log(colors)
53 //数组最后一项索引始终是length-1
54 var colors= ['red', 'blue', 'green']
55 colors[99]= 'black'
56 console.log(colors)//["red", "blue", "green", 99: "black"]
57 console.log(colors.length)//100
58
59
60
61
62
63 /*
64 栈方法
65 栈(LIFO):last-in-fist-out 后进先出的数据结构
66 数组可以表现的像栈一样
67
68 push()方法:
69 可以接收任意数量的参数,把它们逐个添加到末尾,并返回修改后的长度
70
71 pop()方法:
72 从数组末尾移除最后一项,减少数组的长度,并返回移除的项
73 */
74 var colors= new Array()
75 var count= colors.push('red', 'green') //推入两项
76 console.log(count) //2
77 count= colors.push('black') //推入一项
78 console.log(count) //3
79
80 var item= colors.pop() //取得最后一项
81 console.log(item) //back
82 console.log(colors.length) //2
83
84 /*
85 栈方法可以与其他数组方法连用
86 */
87 var colors= ['red', 'blue']
88 colors.push('brown')
89 colors[3]= 'black'
90 console.log(colors.length) //4
91 var item= colors.pop()
92 console.log(item) //black
93
94 /*
95 队列方法
96 队列(FIFO):fist-in-fist-out 先进先出
97 队列在列表末端添加项,在列表前端移除项
98
99 shift()方法
100 从数组前端取得项的方法
101
102 结合使用shift()和push()方法,可以像使用队列一样使用数组
103 */
104 var colors= new Array();
105 var count= colors.push('red', 'green') //推入两项
106 console.log(count) //2
107 var item= colors.shift() //取得第一项
108 console.log(item) //red
109 console.log(colors.length) //1
110
111 /*
112 unshift()
113 与shift()的用途相反,在数组前端添加任意个项,并返回新数组的长度
114
115 结合使用unshift()和pop()方法,可以在数组前端添加项,从数组末端移除项
116 */
117 var colors= new Array()
118 var count= colors.unshift('red', 'green')
119 console.log(count) //2
120 var item= colors.pop()
121 console.log(item)//green
122 console.log(colors.length) // unshift在 iE实现中有偏差
123
124 /*
125 重排序方法
126 1、reverse()
127 */
128 var values= [1, 2, 3, 4, 5]
129 values.reverse()
130 console.log(values) //5,4,3,2,1
131 //2.sort() 升序排列
132 var values= [1, 3, 5, 4, 2]
133 values.sort()
134 console.log(values) //1,2,3,4,5
135
136
137 /*
138 操作方法
139 1.concat() 连接两个或更多的数组
140 */
141 var colors= ['red', 'green', 'blue']
142 var colors2= colors.concat('yellow', ['black', 'brown'])
143 console.log(colors) //"red", "green", "blue"
144 console.log(colors2) //"red", "green", "blue", "yellow", "black", "brown"
145
146 //2.slice() 从某个已有的数组返回选定的元素
147 var colors= ['red', 'green', 'blue', 'yellow', 'brown']
148 var colors2= colors.slice(1)
149 var colors3= colors.slice(1, 4) //从一开始复制到第四项的位置结束
150 console.log(colors2) //["green", "blue", "yellow", "brown"]
151 console.log(colors3) //["green", "blue", "yellow"]
152
153 /*
154 3.splice() 删除元素,并向数组添加新元素。
155 可以删除任意数量的项 指定两个参数,要删除的第一项的位置和要删除的项数
156 可以向指定位置插入任意数量项 指定三个参数 :起始位置,0(要删除的项数),要插入的项
157 可以向指定位置插入任意数量的项
158 */
159 var colors= ['red', 'green', 'blue']
160 var removed= colors.splice(0, 1) //删除第一项
161 console.log(colors) //["green", "blue"]
162 console.log(removed) //["red"]
163
164 removed= colors.splice(1, 0, 'yellow', 'orange')// 从位置1开始插入两项
165 console.log(colors) //["green", "yellow", "orange", "blue"]
166 console.log(removed) //[]
167
168 removed= colors.splice(1, 1, 'red', 'purple') //插入两项,删除一项
169 console.log(colors) //["green", "red", "purple", "orange", "blue"]
170 console.log(removed) //["yellow"]
171
172
173 </script>