Lodash 4.1.6 API學習--Array

作者: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])

獲取指定indexarray數(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]
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市促煮,隨后出現(xiàn)的幾起案子邮屁,更是在濱河造成了極大的恐慌,老刑警劉巖菠齿,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件佑吝,死亡現(xiàn)場離奇詭異,居然都是意外死亡绳匀,警方通過查閱死者的電腦和手機芋忿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來疾棵,“玉大人戈钢,你說我怎么就攤上這事∈嵌” “怎么了殉了?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長拟枚。 經常有香客問我薪铜,道長,這世上最難降的妖魔是什么恩溅? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任痕囱,我火速辦了婚禮,結果婚禮上暴匠,老公的妹妹穿的比我還像新娘鞍恢。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布帮掉。 她就那樣靜靜地躺著弦悉,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蟆炊。 梳的紋絲不亂的頭發(fā)上稽莉,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天,我揣著相機與錄音涩搓,去河邊找鬼污秆。 笑死,一個胖子當著我的面吹牛昧甘,可吹牛的內容都是我干的良拼。 我是一名探鬼主播,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼充边,長吁一口氣:“原來是場噩夢啊……” “哼庸推!你這毒婦竟也來了?” 一聲冷哼從身側響起浇冰,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤贬媒,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后肘习,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體际乘,經...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年漂佩,在試婚紗的時候發(fā)現(xiàn)自己被綠了蚓庭。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡仅仆,死狀恐怖,靈堂內的尸體忽然破棺而出垢袱,到底是詐尸還是另有隱情墓拜,我是刑警寧澤,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布请契,位于F島的核電站咳榜,受9級特大地震影響,放射性物質發(fā)生泄漏爽锥。R本人自食惡果不足惜涌韩,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望氯夷。 院中可真熱鬧臣樱,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至棚放,卻和暖如春枚粘,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背飘蚯。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工馍迄, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人局骤。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓攀圈,卻偏偏與公主長得像,于是被迫代替她去往敵國和親庄涡。 傳聞我的和親對象是個殘疾皇子量承,可洞房花燭夜當晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內容