Lodash——Javascript庫之Array篇

來源:互聯網
上載者:User

標籤:

   這幾天又學到了不少原來不知道的知識,在周末中總結了些Javascript庫—lodash(Array)篇的內容,希望在以後的學習中不時的自己去複習和更新。

 lodash—JavaScript工具庫,現今非常流行,下面是我對它的一些代碼的總結:

1._.chunk  指按照指定的長度去合并數組的元素。

        _.chunk([‘a‘, ‘b‘, ‘c‘, ‘d‘], 2);  

        // → [[‘a‘, ‘b‘], [‘c‘, ‘d‘]]  

        _.chunk([‘a‘, ‘b‘, ‘c‘, ‘d‘], 3);  

        // → [[‘a‘, ‘b‘, ‘c‘], [‘d‘]]  

2._.compact  刪除數組元素中的虛假值。

         _.compact([0, 1, false, 2, ‘‘, 3]);  

        // → [1, 2, 3]  

3._.drop 可以輸出刪除指定位置的元素(預設值為1),若為0,則返回原數組。

        _.drop([1, 2, 3]);  

        // → [2, 3]  

         _.drop([1, 2, 3], 2);  

        // → [3]  

         _.drop([1, 2, 3], 5);  

        // → []                    

        _.drop([1, 2, 3], 0);  

        // → [1, 2, 3]                   //這個就是返回了原數組。

4. _.difference 在第一個數組元素中刪除與第二個數組相同的元素。

        _.difference([1, 2, 3], [5, 2, 10]);  

        // → [1, 3] 

5._.intersetion  通過找到幾個數組中共有的元素,然後進行輸出。

        _.intersection([1, 2, 3], [5, 2, 1, 4], [2, 1]);  

        // → [1, 2]  

6._.dropRight  我自己認為這個和_.drop基本用法相同,只不過這個是從右側進行刪除。

7._.findIndex  可以輸出相反的滿足條件對象的個數。

        var users = [  { ‘user‘: ‘barney‘,  ‘age‘: 36, ‘active‘: false },  { ‘user‘: ‘fred‘,    ‘age‘: 40, ‘active‘: true },                                                      { ‘user‘: ‘pebbles‘, ‘age‘: 1,  ‘active‘: false }  ];  

        _.findIndex(users, function(chr) { return chr.age < 40; });  

        // → 0  

        _.findIndex(users, { ‘age‘: 1 });  

        // → 2 

8. (1.)_.first 輸出第一個元素.

        _.first([1, 2, 3]);  

        // → 1  

        _.first([]);  

        // → undefined  

    (2.)_.rest 輸出除第一個元素的剩下元素。

         _.rest([1, 2, 3]);  

         // → [2, 3]   

    (3)_.last 輸出最後一個元素 

          _.last([1, 2, 3]);  

          // → 3  

    (4.)_.initial 輸出除最後一個元素的剩下元素

          _.initial([1, 2, 3]);  

          // → [1, 2]  

9. 原數組刪除2, 3後輸出.

        var array = [1, 2, 3, 1, 2, 3];  

        _.pull(array, 2, 3);  

        console.log(array);  

        // → [1, 1]

10._.without去掉數組中的元素。

        .without([1, 2, 1, 0, 3, 1, 4], 0, 1);  

        // → [2, 3, 4]  


 










Lodash——Javascript庫之Array篇

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.