JavaScript數組操作函數方法詳解
1、concat() 連接兩個或更多的數組
該方法不會改變現有的數組,而僅僅會返回被連接數組的一個副本。
例如:
1 <script type="text/javascript"> 2 var arr = [1, 2, 3]; 3 var arr1 = [11, 22, 33]; 4 document.write(arr.concat(4, 5, arr1)); 5 </script>
輸出結果:
1,2,3,4,5,11,22,33
2、join()
把數組的所有元素放入一個字符串。元素通過指定的分隔符進行分隔。
例如:
1 <script type="text/javascript"> 2 var arr = ['item 1', 'item 2', 'item 3']; 3 var list = '<ul><li>' + arr.join('</li><li>') + '</li></ul>'; 4 </script>
list結果:
‘<ul><li>item 1</li><li>item 2</li><li>item 3</li></ul>’
這是迄今為止最快的方法!使用原生代碼(如 join()),不管系統內部做了什么,通常比非原生快很多。——James Padolsey, james.padolsey.com
3、pop() 刪除并返回數組的最后一個元素
pop()方法將刪除數組的最后一個元素,把數組長度減 1,并且返回它刪除的元素的值。
如果數組已經為空,則pop()不改變數組,并返回undefined值
例如:
1 <script type="text/javascript"> 2 var arr = ["George", "John", "Thomas"]; 3 document.write(arr + "<br/>"); 4 document.write(arr.pop() + "<br/>"); 5 document.write(arr); 6 </script>
輸出結果:
George,John,Thomas
Thomas
George,John
4、push() 向數組的末尾添加一個或更多元素,并返回新的長度
例如:
1 <script type="text/javascript"> 2 var arr = ["George", "John", "Thomas"]; 3 document.write(arr + "<br/>"); 4 document.write(arr.push("James") + "<br/>"); 5 document.write(arr); 6 </script>
輸出結果:
George,John,Thomas
4
George,John,Thomas,James
5、unshift() 向數組的開頭添加一個或更多元素,并返回新的長度
例如:
1 <script type="text/javascript"> 2 var arr = ["George", "John", "Thomas"]; 3 document.write(arr + "<br/>"); 4 document.write(arr.unshift("James") + "<br/>"); 5 document.write(arr); 6 </script>
輸出結果:
George,John,Thomas
4
James,George,John,Thomas
6、reverse() 顛倒數組中元素的順序
例如:
1 <script type="text/javascript"> 2 var arr = ["George", "John", "Thomas"]; 3 document.write(arr + "<br/>"); 4 document.write(arr.reverse()); 5 </script>
輸出結果:
George,John,Thomas
Thomas,John,George
7、shift() 刪除并返回數組的第一個元素
例如:
1 <script type="text/javascript"> 2 var arr = ["George", "John", "Thomas"]; 3 document.write(arr + "<br/>"); 4 document.write(arr.shift() + "<br/>"); 5 document.write(arr); 6 </script>
輸出結果:
George,John,Thomas
George
John,Thomas
8、slice(start,end) 從某個已有的數組返回選定的元素
請注意,該方法并不會修改數組,而是返回一個子數組
例如:
1 <script type="text/javascript"> 2 var arr = ["George", "John", "Thomas"]; 3 document.write(arr + "<br/>"); 4 document.write(arr.slice(1) + "<br/>"); // 從第一個元素開始截取到 數組結尾 5 document.write(arr); 6 </script>
輸出結果:
George,John,Thomas
John,Thomas
George,John,Thomas
9、sort() 對數組的元素進行排序
對數組的引用。請注意,數組在原數組上進行排序,不生成副本
該方法默認是按照字符編碼(ASCII)的順序進行排序的
例如:
1 <script type="text/javascript"> 2 var arr = new Array(6); 3 arr[0] = "John"; 4 arr[1] = "George"; 5 arr[2] = "Thomas"; 6 document.write(arr + "<br/>"); 7 document.write(arr.sort()); 8 </script>
輸出結果:
John,George,Thomas
George,John,Thomas
再來看一個例子:
1 <script type="text/javascript"> 2 var arr = new Array(6); 3 arr[0] = 10 4 arr[1] = 5 5 arr[2] = 40 6 arr[3] = 25 7 arr[4] = 1000 8 arr[5] = 1 9 document.write(arr + "<br/>"); 10 document.write(arr.sort()); 11 </script>
輸出結果:
10,5,40,25,1000,1
1,10,1000,25,40,5
我們可以看到,并非是按照我們認為的按數字大小排序,如果想按照數字大小排序,則需要改變默認的排序方式,自行指定排序規則。
如下:
1 <script type="text/javascript"> 2 var arr = new Array(6); 3 arr[0] = 10 4 arr[1] = 5 5 arr[2] = 40 6 arr[3] = 25 7 arr[4] = 1000 8 arr[5] = 1 9 document.write(arr + "<br/>"); 10 document.write(arr.sort(function (a, b) {return a - b;}));// 從大到小 11 </script>
輸出結果:
10,5,40,25,1000,1
1,5,10,25,40,1000
如果想要降序排列呢?
將排序規則改為:
function (a, b) {return b – a;}
就OK了
10、splice() 刪除元素,并向數組添加新元素
splice() 方法與 slice() 方法的作用是不同的,splice() 方法會直接對數組進行修改
(1)刪除指定范圍的數組元素:
1 <script type="text/javascript"> 2 var arr = new Array(6); 3 arr[0] = "George"; 4 arr[1] = "John"; 5 arr[2] = "Thomas"; 6 arr[3] = "James"; 7 arr[4] = "Adrew"; 8 arr[5] = "Martin"; 9 10 document.write(arr + "<br/>"); 11 arr.splice(2, 3); // 刪除第三個元素以后的三個數組元素(包含第三個元素) 12 document.write(arr); 13 </script>
輸出結果:
George,John,Thomas,James,Adrew,Martin
George,John,Martin
(2)從指定下標開始插入指定元素(元素個數不限):
1 <script type="text/javascript"> 2 var arr = new Array(6); 3 arr[0] = "George"; 4 arr[1] = "John"; 5 arr[2] = "Thomas"; 6 arr[3] = "James"; 7 arr[4] = "Adrew"; 8 arr[5] = "Martin"; 9 10 document.write(arr + "<br/>"); 11 arr.splice(2, 0, "William","JACK"); // 在第三個元素之前插入"William","JACK" 12 document.write(arr); 13 </script>
輸出結果:
George,John,Thomas,James,Adrew,Martin
George,John,William,JACK,Thomas,James,Adrew,Martin
(3)刪除指定范圍的數組元素,并用指定元素替換(元素個數不限):
1 <script type="text/javascript"> 2 var arr = new Array(6); 3 arr[0] = "George"; 4 arr[1] = "John"; 5 arr[2] = "Thomas"; 6 arr[3] = "James"; 7 arr[4] = "Adrew"; 8 arr[5] = "Martin"; 9 10 document.write(arr + "<br/>"); 11 arr.splice(2,3,"William","JACK"); // 刪除第三個元素以后的三個數組元素(包含第三個元素),并用"William","JACK"進行替換 12 document.write(arr); 13 </script>
輸出結果:
George,John,Thomas,James,Adrew,Martin
George,John,William,JACK,Martin
來源:小小程序猿的博客