做者: Yeaseon
Blog:yeaseonzhang.github.io
原文連接git
Lodash是一個具備一致接口、模塊化、高性能等特性的 JavaScript 工具庫。還有一個相似的庫是underscore,不過underscore不能按需加載。
朋友說:這種工具庫,平時都是先用先寫的。
不過我以爲,仍是頗有必要學習一下的,看下源碼的實現。
本文主要是對Lodash的API,用本身理解的方式作一下說明。可能理解有誤,不過仍是要記錄下來,當再用的時候有據可查。github
_.chunk
_.chunk(array, [size=1])
將數組進行分塊,按照size
指定的長度,默認長度1
數組
_.compact
_.compact(array)
剔除數組中沒有意義的值,好比false, null, 0, "", undefined 和 NaN
bash
_.concat
_.concat(array, [values])
建立一個新的數組來保存原始數組,增長值/數組以後的結果ide
例子:模塊化
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])
這個函數就比較複雜了,就想看一下源碼,發現嵌套的函數太多。就投機取巧直接測試吧。這個函數,大概的做用就是將array
和[values]
進行比較,將array
中比[values]
中多出的值,保存到一個新的數組中。函數
例子:工具
//官網就比較敷衍了
_.difference([2, 1], [2, 3]);
// => [1]
//下面是我作的一些測試
_.difference([1, 1, "1", 2, 2], [1]);
// => ["1", 2, 2]
// 只要array中比[values]中多出的值,都會返回,無論個數出現了幾回複製代碼
_.differenceBy
_.differenceBy(array, [values], [iteratee=_.identity])
這個方法就是在_.difference
方法的基礎上,增長了一個參數。反正是看了一會,纔看明白這個第三個參數,怎麼工做的。性能
例子:學習
_.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
在對獲得的結果進行Math.floor
運算,實際上是想錯了。 若是這麼工做的話,就不必設計_.differenceBy
了,直接用_.difference.xx()
就能夠,因此我一開始的想固然是有問題的。
正確地工做步驟是,對前兩個參數,分別執行第三個參數過濾,而後再比較找出array
中比[values]
中多出的部分,而後返回這些多餘部分的原始值的一個數組。
就拿第一個說吧,執行過濾以後是[2,1]
和[2,3]
,應該是返回[1]
的原始值[1.2]
,就醬。
_.differenceWith
_.differenceWith(array, [values], [comparator])
沒太看懂。。。
_.drop
_.drop(array, [n=1])
我理解的是拋棄前n個數組元素,返回剩下的數組元素,默認拋棄第一個。
例子:
_.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個數組元素,默認拋棄最後一個。
例子:
_.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])
從尾端查詢數組array
,第一個不知足predicate
條件的元素開始截取數組。
參數predicate
提供的是一個屬性名稱,就經過提供的參數使用_.property
方法返回一個回調函數。
參數predicate
提供的是一個對象,就用_.matches
方法匹配相同的元素,相同返回true
,不一樣返回false
。
參數predicate
也能夠提供一個函數,該函數有三個參數value, index, array
_.dropRightWhile
這個函數還牽扯到另外兩個函數,_.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
不一樣之處,是從數組的首端開始查詢。
_.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
數組的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)
這個函數的做用是將array
減小一個維度。
例子:
_.flatten([1, [2, [3, [4]], 5]]);
// => [1, 2, [3, [4]], 5]複製代碼
_.flattenDeep
_.flattenDeep(array)
至關於遞歸執行_.flatten
,最終將array
變成一維數組。
例子:
_.flattenDeep([1, [2, [3, [4]], 5]]);
// => [1, 2, 3, 4, 5]複製代碼
_.flattenDepth
_.flattenDepth(array, [depth=1])
至關於指定執行_.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
鍵值對轉換成一個對象。
例子:
_.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
數組元素。
例子:
var array = ['a', 'b', 'c', 'd'];
_.nth(array, 1);
// => 'b'
_.nth(array, -2);
// => 'c';複製代碼
_.tail
_.tail(array)
返回去除第一個元素的數組。
例子:
_.tail([1, 2, 3]);
// => [2, 3]複製代碼
_.indexOf
_.indexOf(array, value, [fromIndex=0])
在array
中查找value
,返回找到的第一個匹配的index
,沒找到則返回-1
,第三個參數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])
取出各數組中全等的元素,使用SameValueZero
方式平等比較。
例子:
_.intersection([2, 1], [2, 3]);
// => [2]
_.intersection([1, 2], [4, 2], [2, 1]);
// => [2]複製代碼
_.intersectionBy
_.intersectionBy([arrays], [iteratee=_.identity])
_.intersectionBy
就是在_.intersection
的基礎上接受了一個iteratee
迭代器,生成了一個比較的標準,相似於_.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])
這個函數和_.differenceWith
差很少,同樣沒太看懂。
先略過。
_.join
_.join(array, [separator=','])
將array
轉換成字符串類型並經過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
,須要注意的是這個函數會對原始array
作修改。
例子:
var array = ['a', 'b', 'c', 'a', 'b', 'c'];
_.pull(array, 'a', 'c');
console.log(array);
// => ['b', 'b']複製代碼
_.pullAll
_.pullAll(array, values)
_.pullAll
方法應該是_.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
,爲每個數組元素執行迭代器並生成一個比較的標準,這個迭代器調用一個參數value
。
注:原始數組改變
例子:
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])
移除相應index
的元素,返回被移除元素的數組。
注:原始數組改變
例子:
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
返回真的數組元素,並返回被移除的數組元素。predicate
調用三個參數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])
對數組進行分割。
例子:
var array = [1, 2, 3];
_.slice(array, 1, 2);
// => [2]複製代碼
_.sortedIndex
_.sortedIndex(array, value)
value
,將返回這個值插入以後的有序位置。(使用二分查找)例子:
_.sortedIndex([30, 50], 40);
// => 1
_.sortedIndex([30, 50], 30);
// => 0複製代碼
_.sortedIndexBy
_.sortedIndexBy(array, value, [iteratee=_.identity])
凡是帶By
的方法方法,都是這種結構的函數。_.sortIndexBy
比_.sortIndex
方法多一個參數,接收一個迭代器iteratee
去計算排序,這個iteratee
調用一個參數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
是對一個有序數組進行二分查找。
例子:
_.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
,只不過多了一個參數iteratee
,這個迭代器爲每一個元素值計算他們的排序,這個迭代器調用一個參數value
。返回應該被插入後的數組下標。
例子:
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
,只不過它對一個有序數組進行二分查找。
例子:
_.sortedLastIndexOf([4, 5, 5, 5, 6], 5);
// => 3複製代碼
_.sortedUniq
_.sortedUniq(array)
這個方法很像_.uniq
,這個方法是爲了有序數組設計且優化的,返回一個去重的數組。
例子:
_.sortedUniq([1, 1, 2]);
// => [1, 2]複製代碼
_.sortedUniqBy
_.sortedUniqBy(array, [iteratee])
這個方法很像_.uniqBy
,它返回通過iteratee
計算以後,去除重複值,只返回重複值的第一個原值和不重複值組成的有序數組。
例子:
_.sortedUniqBy([1.1, 1.2, 2.3, 2.4], Math.floor);
// => [1.1, 2.3]複製代碼
_.take
_.take(array, [n=1])
建立一個分割後的數組,從array
數組的開始到第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])
建立一個分割後的數組,從array
數組的結尾開始,分割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
結尾開始分割數組,不過是經過predicate
控制,直到返回falsey中止。predicate
調用三個參數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
開頭開始分割數組,不過是經過predicate
控制,直到返回falsey中止。predicate
調用三個參數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])
建立一個沒有重複值的數組,組合全部被傳入的數組元素。
例子:
_.union([2], [1, 2]);
// => [2, 1]複製代碼
_.unionBy
_.unionBy([arrays], [iteratee=_.identity])
經過iteratee
對每一個元素值進行執行,生成一個惟一性的標準,並選擇第一個出現的值,做爲要返回的值,去除重複的元素。iteratee
調用一個參數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)
數組去重。
例子:
_.uniq([2, 1, 2]);
// => [2, 1]複製代碼
_.uniqBy
_.uniqBy(array, [iteratee=_.identity])
這個方法是有條件的數組去重,經過iteratee
迭代器生成一個惟一性的標準。iteratee
調用一個參數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
之間有什麼區別。_.uniqWith
傳入的是一個比較器。comparator
調用兩個參數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])
建立一個新的數組,去除全部傳入的values
。
例子:
_.without([2, 1, 2, 3], 1, 2);
// => [3]複製代碼
_.xor
_.xor([arrays])
xor就是異或,相同爲0,不一樣爲1,1爲true
,應該被返回。
建立一個惟一值的數組,返回被給數組之間對稱差(沒有交集的部分)的元素。結果值的順序由它們在數組中出現的順序肯定。
例子:
_.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
方法生成一個標準,iteratee
接受一個參數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
,它被調用來比較數組的元素。結果值的順序由它們在數組中出現的順序肯定。comparator
調用兩個參數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])
建立一個元素組數組,把每一個傳入數組,第一個元素組合到一塊兒,第二個元素組合在一塊兒,以此類推。
例子:
_.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
函數打包好的,或是元素組數組,對其進行解包。
例子:
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
接受兩個數組,一個屬性數組和一個相應的對應值數組。
例子:
_.zipObject(['a', 'b'], [1, 2]);
// => { 'a': 1, 'b': 2 }
_.zipObject(['a', 'b'], [1]);
// => { 'a': 1, 'b': undefined }複製代碼
_.zipObjectDeep
這個方法像_.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
去指定怎麼如何組合分組值。這個迭代器爲每一個組的元素調用...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
調用一個參數...group
。
例子:
var zipped = _.zip([1, 2], [10, 20], [100, 200]);
// => [[1, 10, 100], [2, 20, 200]]
_.unzipWith(zipped, _.add);
// => [3, 30, 300]複製代碼