作者: Yeaseon
Blog:yeaseonzhang.github.io
原文連結
Lodash是一個具有一緻接口、子產品化、高性能等特性的 JavaScript 工具庫。還有一個類似的庫是underscore,不過underscore不能按需加載。
朋友說:這種工具庫,平時都是先用先寫的。
不過我覺得,還是很有必要學習一下的,看下源碼的實作。
本文主要是對Lodash的API,用自己了解的方式做一下說明。可能了解有誤,不過還是要記錄下來,當再用的時候有據可查。
_.chunk
_.chunk
-
_.chunk(array, [size=1])
将數組進行分塊,按照
size
指定的長度,預設長度
1
_.compact
_.compact
-
_.compact(array)
剔除數組中沒有意義的值,比如
false, null, 0, "", undefined 和 NaN
_.concat
_.concat
-
_.concat(array, [values])
建立一個新的數組來儲存原始數組,增加值/數組之後的結果
例子:
var array = [1];
var other = _.concat(array, 2, [3], [[4]]);
console.log(other);
// => [1, 2, 3, [4]]
console.log(array);
// => [1]複制代碼
_.difference
_.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
-
_.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
-
_.differenceWith(array, [values], [comparator])
沒太看懂。。。
_.drop
_.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
-
_.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
-
_.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
-
_.dropWhile(array, [predicate=_.identity])
這個方法與上面
_.dropRightWhile
不同之處,是從數組的首端開始查詢。
_.fill
_.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
-
_.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
-
_.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
-
_.flatten(array)
這個函數的作用是将
array
減少一個次元。
例子:
_.flatten([1, [2, [3, [4]], 5]]);
// => [1, 2, [3, [4]], 5]複制代碼
_.flattenDeep
_.flattenDeep
-
_.flattenDeep(array)
相當于遞歸執行
_.flatten
,最終将
array
變成一維數組。
例子:
_.flattenDeep([1, [2, [3, [4]], 5]]);
// => [1, 2, 3, 4, 5]複制代碼
_.flattenDepth
_.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
-
_.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
-
_.head(array)
傳回
array
的第一個元素,别名
_.first
例子:
_.head([1, 2, 3]);
// => 1
_.head([]);
// => undefined
_.head([[1, 4], 2, 3]);
// => [1, 4]複制代碼
_.last
_.last
-
_.last(array)
傳回
array
的最後一個元素。
例子:
_.last([1, 2, 3]);
// => 3複制代碼
_.nth
_.nth
-
_.nth(array, [n=0])
擷取指定
index
的
array
數組元素。
例子:
var array = ['a', 'b', 'c', 'd'];
_.nth(array, 1);
// => 'b'
_.nth(array, -2);
// => 'c';複制代碼
_.tail
_.tail
-
_.tail(array)
傳回去除第一個元素的數組。
例子:
_.tail([1, 2, 3]);
// => [2, 3]複制代碼
_.indexOf
_.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
-
_.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
-
_.initial(array)
去除
array
最後一個元素,并傳回。
例子:
_.initial([1, 2, 3]);
// => [1, 2]
_.initial([1, 2, 3, [4, 5]]);
// => [1, 2, 3]複制代碼
_.intersection
_.intersection
-
_.intersection([arrays])
取出各數組中全等的元素,使用
SameValueZero
方式平等比較。
例子:
_.intersection([2, 1], [2, 3]);
// => [2]
_.intersection([1, 2], [4, 2], [2, 1]);
// => [2]複制代碼
_.intersectionBy
_.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
-
_.intersectionWith([arrays], [comparator])
這個函數和
_.differenceWith
差不多,一樣沒太看懂。
先略過。
_.join
_.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
-
_.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
-
_.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
-
_.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
-
_.pullAllWith(array, values, [comoarator])
這個跳過。。。
_.pullAt
_.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
-
_.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
-
_.reverse(array)
這個就比較簡單了,是一個反序排列的方法,也會對原始方法進行更改
例子:
var array = [1, 2, 3];
_.reverse(array);
// => [3, 2, 1]
console.log(array);
// => [3, 2, 1]複制代碼
_.slice
_.slice
-
_.slice(array, [start=0], [end=array.length])
對數組進行分割。
例子:
var array = [1, 2, 3];
_.slice(array, 1, 2);
// => [2]複制代碼
_.sortedIndex
_.sortedIndex
-
向一個有序數組中插入一個_.sortedIndex(array, value)
,将傳回這個值插入之後的有序位置。(使用二分查找)value
例子:
_.sortedIndex([30, 50], 40);
// => 1
_.sortedIndex([30, 50], 30);
// => 0複制代碼
_.sortedIndexBy
_.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
-
_.sortedIndexOf(array, value)
這個方法很像
_.indexOf
,
_.sortedIndexOf
是對一個有序數組進行二分查找。
例子:
_.sortedIndexOf([4, 5, 5, 5, 6], 5);
// => 1複制代碼
_.sortedLastIndex
_.sortedLastIndex
-
_.sortedLastIndex(array, value)
這個方法很像
_.sortedIndex
,這個方法在保持有序的前提下會把
value
插進最大的那個位置。
例子:
_.sortedLastIndex([4, 5, 5, 5, 6], 5);
// => 4複制代碼
_.sortedLastIndexBy
_.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
-
_.sortedLastIndexOf(array, value)
這個方法很像
_.lastIndexOf
,隻不過它對一個有序數組進行二分查找。
例子:
_.sortedLastIndexOf([4, 5, 5, 5, 6], 5);
// => 3複制代碼
_.sortedUniq
_.sortedUniq
-
_.sortedUniq(array)
這個方法很像
_.uniq
,這個方法是為了有序數組設計且優化的,傳回一個去重的數組。
例子:
_.sortedUniq([1, 1, 2]);
// => [1, 2]複制代碼
_.sortedUniqBy
_.sortedUniqBy
-
_.sortedUniqBy(array, [iteratee])
這個方法很像
_.uniqBy
,它傳回經過
iteratee
計算之後,去除重複值,隻傳回重複值的第一個原值和不重複值組成的有序數組。
例子:
_.sortedUniqBy([1.1, 1.2, 2.3, 2.4], Math.floor);
// => [1.1, 2.3]複制代碼
_.take
_.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
-
_.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
-
_.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
-
_.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
-
_.union([arrays])
建立一個沒有重複值的數組,組合所有被傳入的數組元素。
例子:
_.union([2], [1, 2]);
// => [2, 1]複制代碼
_.unionBy
_.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
-
_.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
-
_.uniq(array)
數組去重。
例子:
_.uniq([2, 1, 2]);
// => [2, 1]複制代碼
_.uniqBy
_.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
-
_.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
-
_.without(array, [values])
建立一個新的數組,去除所有傳入的
values
。
例子:
_.without([2, 1, 2, 3], 1, 2);
// => [3]複制代碼
_.xor
_.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
-
_.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
-
_.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
-
_.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
-
_.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
-
_.zipObject([props=[]], [values=[]])
這個方法很像
_.fromPairs
,
_.zipObject
接受兩個數組,一個屬性數組和一個相應的對應值數組。
例子:
_.zipObject(['a', 'b'], [1, 2]);
// => { 'a': 1, 'b': 2 }
_.zipObject(['a', 'b'], [1]);
// => { 'a': 1, 'b': undefined }複制代碼
_.zipObjectDeep
_.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
-
_.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
-
_.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]
作者:Yeaseon_Zhang
連結:https://juejin.im/post/59df3a8b6fb9a0450166f557
來源:掘金
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。