作者:Yeaseon
Lodash是一個具有一致接口、模塊化洋腮、高性能等特性的 JavaScript 工具庫箫柳。還有一個類似的庫是underscore,不過underscore不能按需加載啥供。
朋友說:這種工具庫悯恍,平時都是先用先寫的。
不過我覺得伙狐,還是很有必要學習一下的涮毫,看下源碼的實現(xiàn)瞬欧。
本文主要是對Lodash的API,用自己理解的方式做一下說明≈习伲可能理解有誤黍判,不過還是要記錄下來,當再用的時候有據可查篙梢。
_.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ù)組之后的結果
例子:
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ā)現(xiàn)嵌套的函數(shù)太多。就投機取巧直接測試吧嗽测。這個函數(shù)绪励,大概的作用就是將array
和[values]
進行比較,將array
中比[values]
中多出的值唠粥,保存到一個新的數(shù)組中疏魏。
例子:
//官網就比較敷衍了
_.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
方法的基礎上大莫,增加了一個參數(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
在對得到的結果進行Math.floor
運算,其實是想錯了钠右。 如果這么工作的話赋元,就沒必要設計_.differenceBy
了,直接用_.difference.xx()
就可以爬舰,所以我一開始的想當然是有問題的们陆。
正確地工作步驟是,對前兩個參數(shù)情屹,分別執(zhí)行第三個參數(shù)過濾坪仇,然后再比較找出array
中比[values]
中多出的部分,然后返回這些多余部分的原始值的一個數(shù)組垃你。
就拿第一個說吧椅文,執(zhí)行過濾之后是[2,1]
和[2,3]
喂很,應該是返回[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
方法返回一個回調函數(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)
相當于遞歸執(zhí)行_.flatten
,最終將array
變成一維數(shù)組划煮。
例子:
_.flattenDeep([1, [2, [3, [4]], 5]]);
// => [1, 2, 3, 4, 5]
_.flattenDepth
_.flattenDepth(array, [depth=1])
相當于指定執(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
鍵值對轉換成一個對象。
例子:
_.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
的基礎上接受了一個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])
這個函數(shù)和_.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
,需要注意的是這個函數(shù)會對原始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
面褐,為每一個數(shù)組元素執(zhí)行迭代器并生成一個比較的標準,這個迭代器調用一個參數(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])
移除相應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
調用三個參數(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
的方法方法帽撑,都是這種結構的函數(shù)。
_.sortIndexBy
比_.sortIndex
方法多一個參數(shù)鞍时,接收一個迭代器iteratee
去計算排序亏拉,這個iteratee
調用一個參數(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
,這個迭代器為每個元素值計算他們的排序,這個迭代器調用一個參數(shù)value
灵再。返回應該被插入后的數(shù)組下標肋层。
例子:
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ù)組設計且優(yōu)化的,返回一個去重的數(shù)組汪榔。
例子:
_.sortedUniq([1, 1, 2]);
// => [1, 2]
_.sortedUniqBy
_.sortedUniqBy(array, [iteratee])
這個方法很像_.uniqBy
蒲拉,它返回經過iteratee
計算之后,去除重復值痴腌,只返回重復值的第一個原值和不重復值組成的有序數(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ù)組的結尾開始,分割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
結尾開始分割數(shù)組雨涛,不過是通過predicate
控制,直到返回falsey停止懦胞。predicate
調用三個參數(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
調用三個參數(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)建一個沒有重復值的數(shù)組蚯根,組合所有被傳入的數(shù)組元素。
例子:
_.union([2], [1, 2]);
// => [2, 1]
_.unionBy
_.unionBy([arrays], [iteratee=_.identity])
通過iteratee
對每個元素值進行執(zhí)行,生成一個唯一性的標準颅拦,并選擇第一個出現(xiàn)的值蒂誉,作為要返回的值,去除重復的元素距帅。iteratee
調用一個參數(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
迭代器生成一個唯一性的標準蹂窖。iteratee
調用一個參數(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
調用兩個參數(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
,應該被返回绽乔。
創(chuàng)建一個唯一值的數(shù)組弧蝇,返回被給數(shù)組之間對稱差(沒有交集的部分)的元素。結果值的順序由它們在數(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
方法生成一個標準睦授,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
去枷,它被調用來比較數(shù)組的元素怖辆。結果值的順序由它們在數(shù)組中出現(xiàn)的順序確定。comparator
調用兩個參數(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ù)組和一個相應的對應值數(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
去指定怎么如何組合分組值。這個迭代器為每個組的元素調用...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
調用一個參數(shù)...group
嘶居。
例子:
var zipped = _.zip([1, 2], [10, 20], [100, 200]);
// => [[1, 10, 100], [2, 20, 200]]
_.unzipWith(zipped, _.add);
// => [3, 30, 300]