作者:Yeaseon
Lodash是一個具有一致接口、模塊化、高性能等特性的 JavaScript 工具庫。還有一個類似的庫是underscore,不過underscore不能按需加載。
朋友說:這種工具庫,平時都是先用先寫的。
不過我覺得,還是很有必要學(xué)習(xí)一下的,看下源碼的實現(xiàn)。
本文主要是對Lodash的API,用自己理解的方式做一下說明??赡芾斫庥姓`,不過還是要記錄下來,當(dāng)再用的時候有據(jù)可查。
_.chunk
_.chunk(array, [size=1])
將數(shù)組進行分塊,按照size指定的長度,默認長度1
_.compact
_.compact(array)
剔除數(shù)組中沒有意義的值,比如false, null, 0, "", undefined 和 NaN
_.concat
_.concat(array, [values])
創(chuàng)建一個新的數(shù)組來保存原始數(shù)組,增加值/數(shù)組之后的結(jié)果
例子:
var array = [1];
var other = _.concat(array, 2, [3], [[4]]);
console.log(other);
// => [1, 2, 3, [4]]
console.log(array);
// => [1]
_.difference
_.difference(array, [values])
這個函數(shù)就比較復(fù)雜了,就想看一下源碼,發(fā)現(xiàn)嵌套的函數(shù)太多。就投機取巧直接測試吧。這個函數(shù),大概的作用就是將array和[values]進行比較,將array中比[values]中多出的值,保存到一個新的數(shù)組中。
例子:
//官網(wǎng)就比較敷衍了
_.difference([2, 1], [2, 3]);
// => [1]
//下面是我做的一些測試
_.difference([1, 1, "1", 2, 2], [1]);
// => ["1", 2, 2]
// 只要array中比[values]中多出的值,都會返回,不管個數(shù)出現(xiàn)了幾次
_.differenceBy
_.differenceBy(array, [values], [iteratee=_.identity])
這個方法就是在_.difference方法的基礎(chǔ)上,增加了一個參數(shù)。反正是看了一會,才看明白這個第三個參數(shù),怎么工作的。
例子:
_.differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor);
// => [1.2]
// The `_.property` iteratee shorthand.
_.differenceBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], 'x');
// => [{ 'x': 2 }]
第一個官方例子,就是看了半天沒看懂。我以為是先_.difference在對得到的結(jié)果進行Math.floor運算,其實是想錯了。 如果這么工作的話,就沒必要設(shè)計_.differenceBy了,直接用_.difference.xx()就可以,所以我一開始的想當(dāng)然是有問題的。
正確地工作步驟是,對前兩個參數(shù),分別執(zhí)行第三個參數(shù)過濾,然后再比較找出array中比[values]中多出的部分,然后返回這些多余部分的原始值的一個數(shù)組。
就拿第一個說吧,執(zhí)行過濾之后是[2,1]和[2,3],應(yīng)該是返回[1]的原始值[1.2],就醬。
_.differenceWith
_.differenceWith(array, [values], [comparator])
沒太看懂。。。
_.drop
_.drop(array, [n=1])
我理解的是拋棄前n個數(shù)組元素,返回剩下的數(shù)組元素,默認拋棄第一個。
例子:
_.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]
_.dropRight
_.dropRight(array, [n=1])
_.dropRight和_.drop功能是一樣的,就是_.drop是從后往前拋棄n個數(shù)組元素,默認拋棄最后一個。
例子:
_.dropRight([1, 2, 3]);
// => [1, 2]
_.dropRight([1, 2, 3], 2);
// => [1]
_.dropRight([1, 2, 3], 5);
// => []
_.dropRight([1, 2, 3], 0);
// => [1, 2, 3]
_.dropRightWhile
_.dropRightWhile(array, [predicate=_.identity])
從尾端查詢數(shù)組array,第一個不滿足predicate條件的元素開始截取數(shù)組。
參數(shù)predicate提供的是一個屬性名稱,就通過提供的參數(shù)使用_.property方法返回一個回調(diào)函數(shù)。
參數(shù)predicate提供的是一個對象,就用_.matches方法匹配相同的元素,相同返回true,不同返回false。
參數(shù)predicate也可以提供一個函數(shù),該函數(shù)有三個參數(shù)value, index, array
_.dropRightWhile這個函數(shù)還牽扯到另外兩個函數(shù),_.property和_.matches。
例子:
var users = [
{ 'user': 'barney', 'active': true },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': false }
];
_.dropRightWhile(users, function(o) { return !o.active; });
// => objects for ['barney']
// The `_.matches` iteratee shorthand.
_.dropRightWhile(users, { 'user': 'pebbles', 'active': false });
// => objects for ['barney', 'fred']
// The `_.matchesProperty` iteratee shorthand.
_.dropRightWhile(users, ['active', false]);
// => objects for ['barney']
// The `_.property` iteratee shorthand.
_.dropRightWhile(users, 'active');
// => objects for ['barney', 'fred', 'pebbles']
_.dropWhile
_.dropWhile(array, [predicate=_.identity])
這個方法與上面_.dropRightWhile不同之處,是從數(shù)組的首端開始查詢。
_.fill
_.fill(array, value, [start=0], [end=array.length])
將value填充到array中,start默認為0,end默認為array.length。這個就比較好理解了。
例子:
var array = [1, 2, 3];
_.fill(array, 'a');
console.log(array);
// => ['a', 'a', 'a']
_.fill(Array(3), 2);
// => [2, 2, 2]
_.fill([4, 6, 8, 10], '*', 1, 3);
// => [4, '*', '*', 10]
_.findIndex
_.findIndex(array, [predicate=_.identity], [fromIndex=0])
返回滿足predicate條件的一個array數(shù)組的index,也可以指定從哪里開始查詢,沒找到滿足條件的返回-1
例子:
var users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': true }
];
_.findIndex(users, function(o) { return o.user == 'barney'; });
// => 0
// The `_.matches` iteratee shorthand.
_.findIndex(users, { 'user': 'fred', 'active': false });
// => 1
// The `_.matchesProperty` iteratee shorthand.
_.findIndex(users, ['active', false]);
// => 0
// The `_.property` iteratee shorthand.
_.findIndex(users, 'active');
// => 2
_.findLastIndex
_.findLastIndex(array, [predicate=_.identity], [fromIndex=array.length -1])
和_.findIndex基本相同,不過_.findLastIndex是從尾部往首部開始查找。
例子:
var users = [
{ 'user': 'barney', 'active': true },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': false }
];
_.findLastIndex(users, function(o) { return o.user == 'pebbles'; });
// => 2
// The `_.matches` iteratee shorthand.
_.findLastIndex(users, { 'user': 'barney', 'active': true });
// => 0
// The `_.matchesProperty` iteratee shorthand.
_.findLastIndex(users, ['active', false]);
// => 2
// The `_.property` iteratee shorthand.
_.findLastIndex(users, 'active');
// => 0
_.flatten
_.flatten(array)
這個函數(shù)的作用是將array減少一個維度。
例子:
_.flatten([1, [2, [3, [4]], 5]]);
// => [1, 2, [3, [4]], 5]
_.flattenDeep
_.flattenDeep(array)
相當(dāng)于遞歸執(zhí)行_.flatten,最終將array變成一維數(shù)組。
例子:
_.flattenDeep([1, [2, [3, [4]], 5]]);
// => [1, 2, 3, 4, 5]
_.flattenDepth
_.flattenDepth(array, [depth=1])
相當(dāng)于指定執(zhí)行_.flattenDepth``depth次,默認depth為1。
例子:
var array = [1, [2, [3, [4]], 5]];
_.flattenDepth(array, 1);
// => [1, 2, [3, [4]], 5]
_.flattenDepth(array, 2);
// => [1, 2, 3, [4], 5]
_.fromPairs
_.fromPairs(pairs)
將pairs鍵值對轉(zhuǎn)換成一個對象。
例子:
_.fromPairs([['a', 1], ['b', 2]]);
// => { 'a': 1, 'b': 2 }
_.fromPairs([['a', 1], ['b', 2], ['c', ['d', 4]]]);
// => { 'a': 1, 'b': 2, 'c': [ 'd', 4 ] }
_.head
_.head(array)
返回array的第一個元素,別名_.first
例子:
_.head([1, 2, 3]);
// => 1
_.head([]);
// => undefined
_.head([[1, 4], 2, 3]);
// => [1, 4]
_.last
_.last(array)
返回array的最后一個元素。
例子:
_.last([1, 2, 3]);
// => 3
_.nth
_.nth(array, [n=0])
獲取指定index的array數(shù)組元素。
例子:
var array = ['a', 'b', 'c', 'd'];
_.nth(array, 1);
// => 'b'
_.nth(array, -2);
// => 'c';
_.tail
_.tail(array)
返回去除第一個元素的數(shù)組。
例子:
_.tail([1, 2, 3]);
// => [2, 3]
_.indexOf
_.indexOf(array, value, [fromIndex=0])
在array中查找value,返回找到的第一個匹配的index,沒找到則返回-1,第三個參數(shù)fromIndex指定查找的起始位置,默認為0;
例子:
_.indexOf([1, 2, 1, 2], 2);
// => 1
_.indexOf([1, 2, 1, 2], 3);
// => -1
// Search from the `fromIndex`.
_.indexOf([1, 2, 1, 2], 2, 2);
// => 3
_.lastIndexOf
_.lastIndexOf(array, value, [fromIndex=array.length-1])
和_.indexOf方法一樣,不過是從尾部開始查找。
例子:
_.lastIndexOf([1, 2, 1, 2], 2);
// => 3
// Search from the `fromIndex`.
_.lastIndexOf([1, 2, 1, 2], 2, 2);
// => 1
_.initial
_.initial(array)
去除array最后一個元素,并返回。
例子:
_.initial([1, 2, 3]);
// => [1, 2]
_.initial([1, 2, 3, [4, 5]]);
// => [1, 2, 3]
_.intersection
_.intersection([arrays])
取出各數(shù)組中全等的元素,使用SameValueZero方式平等比較。
例子:
_.intersection([2, 1], [2, 3]);
// => [2]
_.intersection([1, 2], [4, 2], [2, 1]);
// => [2]
_.intersectionBy
_.intersectionBy([arrays], [iteratee=_.identity])
_.intersectionBy就是在_.intersection的基礎(chǔ)上接受了一個iteratee迭代器,生成了一個比較的標(biāo)準(zhǔn),類似于_.differenceBy。
例子:
_.intersectionBy([2.1, 1.2], [2.3, 3.4], Math.floor);
// => [2.1]
// The `_.property` iteratee shorthand.
_.intersectionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }]
_.intersectionWith
_.intersectionWith([arrays], [comparator])
這個函數(shù)和_.differenceWith差不多,一樣沒太看懂。
先略過。
_.join
_.join(array, [separator=','])
將array轉(zhuǎn)換成字符串類型并通過separator分隔開,默認使用,分隔。
例子:
_.join(['a', 'b', 'c'], '~');
// => 'a~b~c'
_.join(['a', 'b', 'c', ['d', 'e']], '-');
// => 'a-b-c-d,e'
_.pull
_.pull(array, [values])
移除array中所有的指定values,需要注意的是這個函數(shù)會對原始array做修改。
例子:
var array = ['a', 'b', 'c', 'a', 'b', 'c'];
_.pull(array, 'a', 'c');
console.log(array);
// => ['b', 'b']
_.pullAll
_.pullAll(array, values)
_.pullAll方法應(yīng)該是_.pull方法的升級,這個方法是在Lodash 4.0.0中提出的。
例子:
var array = ['a', 'b', 'c', 'a', 'b', 'c'];
_.pullAll(array, ['a', 'c']);
console.log(array);
// => ['b', 'b']
_.pullAllBy
_.pullAllBy(array, values, [iteratee=_.identity])
_.pullAllBy方法很像_.pullAll方法,除了可以接受一個迭代器iteratee,為每一個數(shù)組元素執(zhí)行迭代器并生成一個比較的標(biāo)準(zhǔn),這個迭代器調(diào)用一個參數(shù)value。
注:原始數(shù)組改變
例子:
var array1 = [{ 'x': 1 }, { 'x': 2 }, { 'x': 3 }, { 'x': 1 }];
_.pullAllBy(array1, [{ 'x': 1 }, { 'x': 3 }], 'x');
console.log(array);
// => [{ 'x': 2 }]
var array2 = [{ 'x': 1 }, { 'x': 2 }, { 'x': 3 }, { 'x': 1 }, { 'y': 4}];
_.pullAllBy(array, [{ 'x': 1 }], 'y');
console.log(array);
// => [{ 'y': 4 }]
_.pullAllWith
_.pullAllWith(array, values, [comoarator])
這個跳過。。。
_.pullAt
_.pullAt(array, [indexes])
移除相應(yīng)index的元素,返回被移除元素的數(shù)組。
注:原始數(shù)組改變
例子:
var array = ['a', 'b', 'c', 'd'];
var pulled = _.pullAt(array, [1, 3]);
console.log(array);
// => ['a', 'c']
console.log(pulled);
// => ['b', 'd']
_.remove
_.remove(array, [predicate=_.identity])
移除所有predicate返回真的數(shù)組元素,并返回被移除的數(shù)組元素。predicate調(diào)用三個參數(shù)value, index, array。
例子:
var array = [1, 2, 3, 4];
var evens = _.remove(array, function(n) {
return n % 2 == 0;
});
console.log(array);
// => [1, 3]
console.log(evens);
// => [2, 4]
_.reverse
_.reverse(array)
這個就比較簡單了,是一個反序排列的方法,也會對原始方法進行更改
例子:
var array = [1, 2, 3];
_.reverse(array);
// => [3, 2, 1]
console.log(array);
// => [3, 2, 1]
_.slice
_.slice(array, [start=0], [end=array.length])
對數(shù)組進行分割。
例子:
var array = [1, 2, 3];
_.slice(array, 1, 2);
// => [2]
_.sortedIndex
-
_.sortedIndex(array, value)
向一個有序數(shù)組中插入一個value,將返回這個值插入之后的有序位置。(使用二分查找)
例子:
_.sortedIndex([30, 50], 40);
// => 1
_.sortedIndex([30, 50], 30);
// => 0
_.sortedIndexBy
_.sortedIndexBy(array, value, [iteratee=_.identity])
凡是帶By的方法方法,都是這種結(jié)構(gòu)的函數(shù)。
_.sortIndexBy比_.sortIndex方法多一個參數(shù),接收一個迭代器iteratee去計算排序,這個iteratee調(diào)用一個參數(shù)value。
例子:
var objects = [{ 'x': 4 }, { 'x': 5 }];
_.sortedIndexBy(objects, { 'x': 4 }, function(o) { return o.x; });
// => 0
// The `_.property` iteratee shorthand.
_.sortedIndexBy(objects, { 'x': 4 }, 'x');
// => 0
_.sortedIndexOf
_.sortedIndexOf(array, value)
這個方法很像_.indexOf,_.sortedIndexOf是對一個有序數(shù)組進行二分查找。
例子:
_.sortedIndexOf([4, 5, 5, 5, 6], 5);
// => 1
_.sortedLastIndex
_.sortedLastIndex(array, value)
這個方法很像_.sortedIndex,這個方法在保持有序的前提下會把value插進最大的那個位置。
例子:
_.sortedLastIndex([4, 5, 5, 5, 6], 5);
// => 4
_.sortedLastIndexBy
_.sortedLastIndexBy(array, value, [iteratee=_.identity])
這個方法很像_.sortedLastIndex,只不過多了一個參數(shù)iteratee,這個迭代器為每個元素值計算他們的排序,這個迭代器調(diào)用一個參數(shù)value。返回應(yīng)該被插入后的數(shù)組下標(biāo)。
例子:
var objects = [{ 'x': 4 }, { 'x': 5 }];
_.sortedLastIndexBy(objects, { 'x': 4 }, function(o) { return o.x; });
// => 1
// The `_.property` iteratee shorthand.
_.sortedLastIndexBy(objects, { 'x': 4 }, 'x');
// => 1
_.sortedLastIndexOf
_.sortedLastIndexOf(array, value)
這個方法很像_.lastIndexOf,只不過它對一個有序數(shù)組進行二分查找。
例子:
_.sortedLastIndexOf([4, 5, 5, 5, 6], 5);
// => 3
_.sortedUniq
_.sortedUniq(array)
這個方法很像_.uniq,這個方法是為了有序數(shù)組設(shè)計且優(yōu)化的,返回一個去重的數(shù)組。
例子:
_.sortedUniq([1, 1, 2]);
// => [1, 2]
_.sortedUniqBy
_.sortedUniqBy(array, [iteratee])
這個方法很像_.uniqBy,它返回經(jīng)過iteratee計算之后,去除重復(fù)值,只返回重復(fù)值的第一個原值和不重復(fù)值組成的有序數(shù)組。
例子:
_.sortedUniqBy([1.1, 1.2, 2.3, 2.4], Math.floor);
// => [1.1, 2.3]
_.take
_.take(array, [n=1])
創(chuàng)建一個分割后的數(shù)組,從array數(shù)組的開始到第n個元素。
例子:
_.take([1, 2, 3]);
// => [1]
_.take([1, 2, 3], 2);
// => [1, 2]
_.take([1, 2, 3], 5);
// => [1, 2, 3]
_.take([1, 2, 3], 0);
// => []
_.takeRight
_.takeRight(array, [n=1])
創(chuàng)建一個分割后的數(shù)組,從array數(shù)組的結(jié)尾開始,分割n個元素出來。
例子:
_.takeRight([1, 2, 3]);
// => [3]
_.takeRight([1, 2, 3], 2);
// => [2, 3]
_.takeRight([1, 2, 3], 5);
// => [1, 2, 3]
_.takeRight([1, 2, 3], 0);
// => []
_.takeRightWhile
_.takeRightWhile(array, [predicate=_.identity])
同樣是從array結(jié)尾開始分割數(shù)組,不過是通過predicate控制,直到返回falsey停止。predicate調(diào)用三個參數(shù)value, index, array
例子:
var users = [
{ 'user': 'barney', 'active': true },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': false }
];
_.takeRightWhile(users, function(o) { return !o.active; });
// => objects for ['fred', 'pebbles']
// The `_.matches` iteratee shorthand.
_.takeRightWhile(users, { 'user': 'pebbles', 'active': false });
// => objects for ['pebbles']
// The `_.matchesProperty` iteratee shorthand.
_.takeRightWhile(users, ['active', false]);
// => objects for ['fred', 'pebbles']
// The `_.property` iteratee shorthand.
_.takeRightWhile(users, 'active');
// => []
_.takeWhile
_.takeWhile(array, [predivate=_.identity])
是從array開頭開始分割數(shù)組,不過是通過predicate控制,直到返回falsey停止。predicate調(diào)用三個參數(shù)value, index, array
例子:
var users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': false},
{ 'user': 'pebbles', 'active': true }
];
_.takeWhile(users, function(o) { return !o.active; });
// => objects for ['barney', 'fred']
// The `_.matches` iteratee shorthand.
_.takeWhile(users, { 'user': 'barney', 'active': false });
// => objects for ['barney']
// The `_.matchesProperty` iteratee shorthand.
_.takeWhile(users, ['active', false]);
// => objects for ['barney', 'fred']
// The `_.property` iteratee shorthand.
_.takeWhile(users, 'active');
// => []
_.union
_.union([arrays])
創(chuàng)建一個沒有重復(fù)值的數(shù)組,組合所有被傳入的數(shù)組元素。
例子:
_.union([2], [1, 2]);
// => [2, 1]
_.unionBy
_.unionBy([arrays], [iteratee=_.identity])
通過iteratee對每個元素值進行執(zhí)行,生成一個唯一性的標(biāo)準(zhǔn),并選擇第一個出現(xiàn)的值,作為要返回的值,去除重復(fù)的元素。iteratee調(diào)用一個參數(shù)value。
例子:
_.unionBy([2.1], [1.2, 2.3], Math.floor);
// => [2.1, 1.2]
// The `_.property` iteratee shorthand.
_.unionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }, { 'x': 2 }]
_.unionWith
_.unionWith([arrays], [comparator])
這個沒太看懂。。。
例子:
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
_.unionWith(objects, others, _.isEqual);
// => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
_.uniq
_.uniq(array)
數(shù)組去重。
例子:
_.uniq([2, 1, 2]);
// => [2, 1]
_.uniqBy
_.uniqBy(array, [iteratee=_.identity])
這個方法是有條件的數(shù)組去重,通過iteratee迭代器生成一個唯一性的標(biāo)準(zhǔn)。iteratee調(diào)用一個參數(shù)value.
例子:
_.uniqBy([2.1, 1.2, 2.3], Math.floor);
// => [2.1, 1.2]
// The `_.property` iteratee shorthand.
_.uniqBy([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }, { 'x': 2 }]
_.uniqWith
_.uniqWith(array, [comparator])
沒太分清_.uniqWith和_.uniqBy之間有什么區(qū)別。_.uniqWith傳入的是一個比較器。comparator調(diào)用兩個參數(shù)arrVal, othVal。
例子:
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];
_.uniqWith(objects, _.isEqual);
// => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]
_.without
_.without(array, [values])
創(chuàng)建一個新的數(shù)組,去除所有傳入的values。
例子:
_.without([2, 1, 2, 3], 1, 2);
// => [3]
_.xor
_.xor([arrays])
xor就是異或,相同為0,不同為1,1為true,應(yīng)該被返回。
創(chuàng)建一個唯一值的數(shù)組,返回被給數(shù)組之間對稱差(沒有交集的部分)的元素。結(jié)果值的順序由它們在數(shù)組中出現(xiàn)的順序確定。
例子:
_.xor([2, 1, 4], [2, 3, 5]);
// => [1, 4, 3, 5]
_.xor([2, 2, 3], [4, 4, 5, 6]);
// => [2, 3, 4, 5, 6]
_.xorBy
_.xorBy([arrays], [iteratee=_.identity])
有條件的_.xor方法,和所有_.xxBy方法一樣,接收一個iteratee方法生成一個標(biāo)準(zhǔn),iteratee接受一個參數(shù)value。
例子:
_.xorBy([2.1, 1.2], [2.3, 3.4], Math.floor);
// => [1.2, 3.4]
// The `_.property` iteratee shorthand.
_.xorBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 2 }]
_.xorWith
_.xorWith([arrays], [comparator])
這個方法類似于_.xor,除了它接受比較器comparator,它被調(diào)用來比較數(shù)組的元素。結(jié)果值的順序由它們在數(shù)組中出現(xiàn)的順序確定。comparator調(diào)用兩個參數(shù)arrVal,othVal。
例子:
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
_.xorWith(objects, others, _.isEqual);
// => [{ 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
_.zip
_.zip([arrays])
創(chuàng)建一個元素組數(shù)組,把每個傳入數(shù)組,第一個元素組合到一起,第二個元素組合在一起,以此類推。
例子:
_.zip(['a', 'b'], [1, 2], [true, false]);
// => [['a', 1, true], ['b', 2, false]]
_.zip(['a', 'b'], [1], [true, false]);
// => [['a', 1, true], ['b', undefined, false]]
_.unzip
_.unzip(array)
就是把_.zip函數(shù)打包好的,或是元素組數(shù)組,對其進行解包。
例子:
var zipped = _.zip(['a', 'b'], [1, 2], [true, false]);
// => [['a', 1, true], ['b', 2, false]]
_.unzip(zipped);
// => [['a', 'b'], [1, 2], [true, false]]
_.zipObject
_.zipObject([props=[]], [values=[]])
這個方法很像_.fromPairs,_.zipObject接受兩個數(shù)組,一個屬性數(shù)組和一個相應(yīng)的對應(yīng)值數(shù)組。
例子:
_.zipObject(['a', 'b'], [1, 2]);
// => { 'a': 1, 'b': 2 }
_.zipObject(['a', 'b'], [1]);
// => { 'a': 1, 'b': undefined }
_.zipObjectDeep
- `_.zipObjectDeep([props=[]], [values=[]])
這個方法像_.zipObject方法一樣,不過它支持屬性路徑property paths
例子:
_.zipObjectDeep(['a.b[0].c', 'a.b[1].d'], [1, 2]);
// => { 'a': { 'b': [{ 'c': 1 }, { 'd': 2 }] } }
_.zipWith
_.zipWith([arrays], [iteratee=_.identity])
這個方法像類似_.zip,接受一個迭代器iteratee去指定怎么如何組合分組值。這個迭代器為每個組的元素調(diào)用...group,還是看例子比較直觀。
例子:
_.zipWith([1, 2], [10, 20], [100, 200], function(a, b, c) {
return a + b + c;
});
// => [111, 222]
_.unzipWith
_.unzipWith(array, [iteratee=_.identity])
這個方法很像_.unzip,它接受一個迭代器iteratee去指定怎樣重組組合值。iteratee調(diào)用一個參數(shù)...group。
例子:
var zipped = _.zip([1, 2], [10, 20], [100, 200]);
// => [[1, 10, 100], [2, 20, 200]]
_.unzipWith(zipped, _.add);
// => [3, 30, 300]