這幾天又學到了很多原來不知道的知識,在週末中總結了些Javascript庫—lodash(Array)篇的內容,但願在之後的學習中不時的本身去複習和更新。數組
lodash—JavaScript工具庫,現今很是流行,下面是我對它的一些代碼的總結:工具
1._.chunk 指按照指定的長度去合併數組的元素。學習
_.chunk(['a', 'b', 'c', 'd'], 2); spa
// → [['a', 'b'], ['c', 'd']] rest
_.chunk(['a', 'b', 'c', 'd'], 3); 對象
// → [['a', 'b', 'c'], ['d']] ip
2._.compact 刪除數組元素中的虛假值。it
_.compact([0, 1, false, 2, '', 3]); io
// → [1, 2, 3] console
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]