1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<span style="color: #003366;font-weight: bold">var</span> arr <span style="color: #339933">=</span> <span style="color: #009900">[</span> <span style="color: #CC0000">1</span><span style="color: #339933">,</span> <span style="color: #CC0000">2</span><span style="color: #339933">,</span> <span style="color: #CC0000">3</span><span style="color: #339933">,</span> <span style="color: #CC0000">4</span><span style="color: #339933">,</span> <span style="color: #CC0000">5</span> <span style="color: #009900">]</span><span style="color: #339933">;</span>
<span style="color: #006600;font-style: italic">//原始数组</span>
<span style="color: #000066">alert</span><span style="color: #009900">(</span><span style="color: #3366CC">"原始数组:"</span> <span style="color: #339933">+</span> arr<span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #006600;font-style: italic">// 1,2,3,4,5</span>
<span style="color: #006600;font-style: italic">//删除并且返回第一个元素</span>
<span style="color: #000066">alert</span><span style="color: #009900">(</span><span style="color: #3366CC">"执行arr.shift() 返回 :"</span> <span style="color: #339933">+</span> arr.<span style="color: #660066">shift</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #006600;font-style: italic">//1</span>
<span style="color: #000066">alert</span><span style="color: #009900">(</span><span style="color: #3366CC">"数组:"</span> <span style="color: #339933">+</span> arr<span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #006600;font-style: italic">//2,3,4,5</span>
<span style="color: #006600;font-style: italic">//删除并且返回最后一个元素</span>
<span style="color: #000066">alert</span><span style="color: #009900">(</span><span style="color: #3366CC">"执行arr.pop() 返回:"</span> <span style="color: #339933">+</span> arr.<span style="color: #660066">pop</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #006600;font-style: italic">//5</span>
<span style="color: #000066">alert</span><span style="color: #009900">(</span><span style="color: #3366CC">"数组:"</span> <span style="color: #339933">+</span> arr<span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #006600;font-style: italic">//2,3,4</span>
<span style="color: #006600;font-style: italic">//在数组开头添加一个或者多个元素,返回数组新长度</span>
<span style="color: #000066">alert</span><span style="color: #009900">(</span><span style="color: #3366CC">"执行arr.unshift('one') 返回:"</span> <span style="color: #339933">+</span> arr.<span style="color: #660066">unshift</span><span style="color: #009900">(</span><span style="color: #3366CC">"one"</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #006600;font-style: italic">//4</span>
<span style="color: #000066">alert</span><span style="color: #009900">(</span><span style="color: #3366CC">"数组:"</span> <span style="color: #339933">+</span> arr<span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #006600;font-style: italic">//one,2,3,4</span>
<span style="color: #006600;font-style: italic">//在数组尾部添加一个或者多个元素,返回数组新长度</span>
<span style="color: #000066">alert</span><span style="color: #009900">(</span><span style="color: #3366CC">"执行arr.push('end') 返回:"</span> <span style="color: #339933">+</span> arr.<span style="color: #660066">push</span><span style="color: #009900">(</span><span style="color: #3366CC">"end"</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #006600;font-style: italic">//5</span>
<span style="color: #000066">alert</span><span style="color: #009900">(</span><span style="color: #3366CC">"数组:"</span> <span style="color: #339933">+</span> arr<span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #006600;font-style: italic">//one,2,3,4,end</span>
<span style="color: #006600;font-style: italic">//从第i个位置开始删除n个元素,返</span>
arr.<span style="color: #660066">splice</span><span style="color: #009900">(</span><span style="color: #CC0000">0</span><span style="color: #339933">,</span> <span style="color: #CC0000">1</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000066">alert</span><span style="color: #009900">(</span><span style="color: #3366CC">"执行arr.splice(0,1) "</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000066">alert</span><span style="color: #009900">(</span><span style="color: #3366CC">"数组:"</span> <span style="color: #339933">+</span> arr<span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #006600;font-style: italic">//2,3,4,end</span>
<span style="color: #006600;font-style: italic">//从第i个位置开始,删除n个元素,并且在这个位置插入s个元素</span>
arr.<span style="color: #660066">splice</span><span style="color: #009900">(</span><span style="color: #CC0000">0</span><span style="color: #339933">,</span> <span style="color: #CC0000">0</span><span style="color: #339933">,</span> <span style="color: #3366CC">"one"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000066">alert</span><span style="color: #009900">(</span><span style="color: #3366CC">"执行arr.splice(0,0,'one')"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000066">alert</span><span style="color: #009900">(</span><span style="color: #3366CC">"数组:"</span> <span style="color: #339933">+</span> arr<span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #006600;font-style: italic">//one,2,3,4,end</span>