JS數組(Array)處理函數整理,jsarray

來源:互聯網
上載者:User

JS數組(Array)處理函數整理,jsarray

1、concat() 串連兩個或更多的數組
該方法不會改變現有的數組,而僅僅會返回被串連數組的一個副本。
例如:

複製代碼 代碼如下:
 <script type="text/javascript">
        var arr = [1, 2, 3];
        var arr1 = [11, 22, 33];
        document.write(arr.concat(4, 5, arr1));
 </script>

輸出結果:
1,2,3,4,5,11,22,33

2、join()
把數組的所有元素放入一個字串。元素通過指定的分隔字元進行分隔。
例如:

複製代碼 代碼如下:
 <script type="text/javascript">
       var arr = ['item 1', 'item 2', 'item 3'];
       var list = '<ul><li>' + arr.join('</li><li>') + '</li></ul>';
 </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值
例如:

複製代碼 代碼如下:
 <script type="text/javascript">
       var arr = ["George", "John", "Thomas"];
       document.write(arr + "<br/>");
       document.write(arr.pop() + "<br/>");
       document.write(arr);
 </script>

輸出結果:
George,John,Thomas
Thomas
George,John

4、push() 向數組的末尾添加一個或更多元素,並返回新的長度
例如:

複製代碼 代碼如下:
 <script type="text/javascript">
       var arr = ["George", "John", "Thomas"];
       document.write(arr + "<br/>");
       document.write(arr.push("James") + "<br/>");
       document.write(arr);
 </script>

輸出結果:
George,John,Thomas
4
George,John,Thomas,James

 

5、unshift() 向數組的開頭添加一個或更多元素,並返回新的長度
例如:

複製代碼 代碼如下:
 <script type="text/javascript">
       var arr = ["George", "John", "Thomas"];
       document.write(arr + "<br/>");
       document.write(arr.unshift("James") + "<br/>");
       document.write(arr);
 </script>

輸出結果:
George,John,Thomas
4
James,George,John,Thomas

 

6、reverse() 顛倒數組中元素的順序
例如:

複製代碼 代碼如下:
 <script type="text/javascript">
       var arr = ["George", "John", "Thomas"];
       document.write(arr + "<br/>");
       document.write(arr.reverse());
 </script>

輸出結果:
George,John,Thomas
Thomas,John,George

7、shift() 刪除並返回數組的第一個元素
例如:

複製代碼 代碼如下:
 <script type="text/javascript">
       var arr = ["George", "John", "Thomas"];
       document.write(arr + "<br/>");
       document.write(arr.shift() + "<br/>");
       document.write(arr);
 </script>

輸出結果:
George,John,Thomas
George
John,Thomas

8、slice(start,end) 從某個已有的數組返回選定的元素
請注意,該方法並不會修改數組,而是返回一個子數組
例如:

複製代碼 代碼如下:
 <script type="text/javascript">
       var arr = ["George", "John", "Thomas"];
       document.write(arr + "<br/>");
       document.write(arr.slice(1) + "<br/>"); // 從第一個元素開始截取到 數組結尾
       document.write(arr);
 </script>

輸出結果:
George,John,Thomas
John,Thomas
George,John,Thomas

9、sort() 對數組的元素進行排序
對數組的引用。請注意,數組在原數組上進行排序,不產生副本
該方法預設是按照字元編碼(ASCII)的順序進行排序的
例如:

複製代碼 代碼如下:
 <script type="text/javascript">
     var arr = new Array(6);
     arr[0] = "John";
     arr[1] = "George";
     arr[2] = "Thomas";
     document.write(arr + "<br/>");
     document.write(arr.sort());
 </script>

輸出結果:
John,George,Thomas
George,John,Thomas

再來看一個例子:

複製代碼 代碼如下:
 <script type="text/javascript">
     var arr = new Array(6);
     arr[0] = 10
     arr[1] = 5
     arr[2] = 40
     arr[3] = 25
     arr[4] = 1000
     arr[5] = 1
     document.write(arr + "<br/>");
     document.write(arr.sort());
 </script>

輸出結果:
10,5,40,25,1000,1
1,10,1000,25,40,5

我們可以看到,並非是按照我們認為的按數字大小排序,如果想按照數字大小排序,則需要改變預設的排序方式,自行指定定序。
如下:

複製代碼 代碼如下:
 <script type="text/javascript">
     var arr = new Array(6);
     arr[0] = 10
     arr[1] = 5
     arr[2] = 40
     arr[3] = 25
     arr[4] = 1000
     arr[5] = 1
     document.write(arr + "<br/>");
     document.write(arr.sort(function (a, b) {return a - b;}));// 從大到小
 </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)刪除指定範圍的數組元素:

複製代碼 代碼如下:
 <script type="text/javascript">
     var arr = new Array(6);
    arr[0] = "George";
    arr[1] = "John";
    arr[2] = "Thomas";
    arr[3] = "James";
    arr[4] = "Adrew";
    arr[5] = "Martin";
    document.write(arr + "<br/>");
    arr.splice(2, 3); // 刪除第三個元素以後的三個數組元素(包含第三個元素)
    document.write(arr);
 </script>

輸出結果:
George,John,Thomas,James,Adrew,Martin
George,John,Martin

(2)從指定下標開始插入指定元素(元素個數不限):

複製代碼 代碼如下:
 <script type="text/javascript">
    var arr = new Array(6);
    arr[0] = "George";
    arr[1] = "John";
    arr[2] = "Thomas";
    arr[3] = "James";
    arr[4] = "Adrew";
    arr[5] = "Martin";
    document.write(arr + "<br/>");
    arr.splice(2, 0, "William","JACK"); // 在第三個元素之前插入"William","JACK"
    document.write(arr);
 </script>

輸出結果:
George,John,Thomas,James,Adrew,Martin
George,John,William,JACK,Thomas,James,Adrew,Martin


(3)刪除指定範圍的數組元素,並用指定元素替換(元素個數不限):

複製代碼 代碼如下:
 <script type="text/javascript">
    var arr = new Array(6);
    arr[0] = "George";
    arr[1] = "John";
    arr[2] = "Thomas";
    arr[3] = "James";
    arr[4] = "Adrew";
    arr[5] = "Martin";
    document.write(arr + "<br/>");
    arr.splice(2,3,"William","JACK"); // 刪除第三個元素以後的三個數組元素(包含第三個元素),並用"William","JACK"進行替換
 document.write(arr);
 </script>

輸出結果:
George,John,Thomas,James,Adrew,Martin
George,John,William,JACK,Martin

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.