?
This document uses PHP Chinese website manual Release
創(chuàng)建一個由運行集合中的每個元素到迭代器的結(jié)果生成的鍵組成的對象。每個鍵的相應值是密鑰返回的次數(shù)iteratee
。迭代器調(diào)用一個參數(shù):(value)。
0.5.0
collection
(Array | Object):迭代的集合。
[iteratee=_.identity]
(Function):用于轉(zhuǎn)換密鑰的迭代器。
(Object):返回組合的聚合對象。
_.countBy([6.1, 4.2, 6.3], Math.floor); // => { '4': 1, '6': 2 } // The `_.property` iteratee shorthand. _.countBy(['one', 'two', 'three'], 'length'); // => { '3': 2, '5': 1 }
檢查是否對于collection
所有元素,predicate
返回true 。一旦predicate
返回falsey,迭代就會停止。謂詞用三個參數(shù)調(diào)用:(value,index | key,collection)。
注意:此方法返回true
的空集,因為是一切都是真的空集的元素。
0.1.0
collection
(Array | Object):迭代的集合。
[predicate=_.identity]
(Function):每次迭代調(diào)用的函數(shù)。
(boolean):所有元素是否通過了謂詞檢查則返回true
,否則false
。
_.every([true, 1, null, 'yes'], Boolean); // => false var users = [ { 'user': 'barney', 'age': 36, 'active': false }, { 'user': 'fred', 'age': 40, 'active': false }]; // The `_.matches` iteratee shorthand. _.every(users, { 'user': 'barney', 'active': false }); // => false // The `_.matchesProperty` iteratee shorthand. _.every(users, ['active', false]); // => true // The `_.property` iteratee shorthand. _.every(users, 'active'); // => false
迭代集合的元素,返回謂詞,返回truthy的所有元素的數(shù)組。謂詞用三個參數(shù)調(diào)用:(value,index | key,collection)。
注意: 與_.remove
此不同,此方法返回一個新數(shù)組。
0.1.0
collection
(Array | Object):迭代的集合。
[predicate=_.identity]
(Function):每次迭代調(diào)用的函數(shù)。
(Array):返回新的過濾數(shù)組。
var users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false }]; _.filter(users, function(o) { return !o.active; }); // => objects for ['fred'] // The `_.matches` iteratee shorthand. _.filter(users, { 'age': 36, 'active': true }); // => objects for ['barney'] // The `_.matchesProperty` iteratee shorthand. _.filter(users, ['active', false]); // => objects for ['fred'] // The `_.property` iteratee shorthand. _.filter(users, 'active'); // => objects for ['barney']
迭代集合的元素,返回第一個元素謂詞,返回truthy。 謂詞用三個參數(shù)調(diào)用:(value,index | key,collection)。
0.1.0
collection
(Array | Object):要檢查的集合。
[predicate=_.identity]
(Function):每次迭代調(diào)用的函數(shù)。
[fromIndex=0]
(number):從中搜索的索引。
(*):返回匹配的元素,否則返回undefined
。
var users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': true }]; _.find(users, function(o) { return o.age < 40; }); // => object for 'barney' // The `_.matches` iteratee shorthand. _.find(users, { 'age': 1, 'active': true }); // => object for 'pebbles' // The `_.matchesProperty` iteratee shorthand. _.find(users, ['active', false]); // => object for 'fred' // The `_.property` iteratee shorthand. _.find(users, 'active'); // => object for 'barney'
這個方法就像_.find
,不同的是它遍歷collection
從右到左的元素 。
2.0.0
collection
(Array | Object):要檢查的集合。
[predicate=_.identity]
(Function):每次迭代調(diào)用的函數(shù)。
[fromIndex=collection.length-1]
(number):從中搜索的索引。
(*):返回匹配的元素,否則返回undefined
。
_.findLast([1, 2, 3, 4], function(n) { return n % 2 == 1;});// => 3
通過在迭代中運行collection中的每個元素并壓扁映射的結(jié)果來創(chuàng)建一個扁平化的值數(shù)組。迭代器被調(diào)用三個參數(shù):(value,index | key,collection)。
4.0.0
collection
(Array | Object):迭代的集合。
[iteratee=_.identity]
(Function):每次迭代調(diào)用的函數(shù)。
(Array):返回新的展平數(shù)組。
function duplicate(n) { return [n, n];} _.flatMap([1, 2], duplicate);// => [1, 1, 2, 2]
這種方法就像_.flatMap
,不同的是它遞歸地平整映射結(jié)果。
4.7.0
collection
(Array | Object):迭代的集合。
[iteratee=_.identity]
(Function):每次迭代調(diào)用的函數(shù)。
(Array):返回新的展平數(shù)組。
function duplicate(n) { return [[[n, n]]];} _.flatMapDeep([1, 2], duplicate);// => [1, 1, 2, 2]
這種方法就像_.flatMap
,不同的是它遞歸地將映射的結(jié)果平展到depth
時間。
4.7.0
collection
(Array | Object):迭代的集合。
[iteratee=_.identity]
(Function):每次迭代調(diào)用的函數(shù)。
[depth=1]
(數(shù)字):最大遞歸深度。
(數(shù)組):返回新的展平數(shù)組。
function duplicate(n) { return [[[n, n]]];} _.flatMapDepth([1, 2], duplicate, 2); // => [[1, 1], [2, 2]]
迭代元素collection
與iteratee
為每個元素調(diào)用。迭代器調(diào)用三個參數(shù):(value,index | key,collection)。迭代器函數(shù)可以通過顯式提前退出迭代,返回false
。
注意:與其他“集合”方法一樣,具有“長度”屬性的對象與數(shù)組一樣迭代。為了避免這種行為,使用_.forIn
或_.forOwn
用于對象迭代。
0.1.0
_.each
collection
(Array | Object):迭代的集合。
[iteratee=_.identity]
(Function):每次迭代調(diào)用的函數(shù)。
(*): 返回 collection
.
_.forEach([1, 2], function(value) { console.log(value);}); // => Logs `1` then `2`. _.forEach({ 'a': 1, 'b': 2 }, function(value, key) { console.log(key);}); // => Logs 'a' then 'b' (iteration order is not guaranteed).
這個方法就像_.forEach
,不同的是它遍歷collection
從右到左的元素。
2.0.0
_.eachRight
collection
(Array | Object):迭代的集合。
[iteratee=_.identity]
(Function):每次迭代調(diào)用的函數(shù)。
(*): 返回collection
.
_.forEachRight([1, 2], function(value) { console.log(value);}); // => Logs `2` then `1`.
創(chuàng)建一個由運行集合中的每個元素到迭代器的結(jié)果生成的鍵組成的對象。 分組值的順序由它們在收集中發(fā)生的順序決定。每個鍵的相應值是負責生成鍵的元素數(shù)組。迭代器被調(diào)用一個參數(shù):(value)。
0.1.0
collection
(Array | Object):迭代的集合。
[iteratee=_.identity]
(函數(shù)):用于轉(zhuǎn)換密鑰的迭代器。
(Object):返回組合的聚合對象。
_.groupBy([6.1, 4.2, 6.3], Math.floor); // => { '4': [4.2], '6': [6.1, 6.3] } // The `_.property` iteratee shorthand. _.groupBy(['one', 'two', 'three'], 'length'); // => { '3': ['one', 'two'], '5': ['three'] }
檢查值是否在集合中。 如果collection是一個字符串,則檢查是否有值的子字符串,否則使用SameValueZero進行相等性比較。 如果fromIndex為負數(shù),它將用作從收集結(jié)束的偏移量。
0.1.0
collection
(Array | Object | string):要檢查的集合。
value
(*):要搜索的值。
[fromIndex=0]
(數(shù)字):從中搜索的索引。
(boolean): 如果找到值,則返回true,否則返回false。
_.includes([1, 2, 3], 1);// => true _.includes([1, 2, 3], 1, 2);// => false _.includes({ 'a': 1, 'b': 2 }, 1);// => true _.includes('abcd', 'bc');// => true
在集合中每個元素的路徑上調(diào)用該方法,返回每個調(diào)用方法的結(jié)果數(shù)組。 任何額外的參數(shù)都提供給每個調(diào)用的方法。 如果path是一個函數(shù),那么它將被調(diào)用并綁定到集合中的每個元素。
4.0.0
collection
(Array | Object):迭代的集合。
path
(Array | Function | string):要調(diào)用的方法的路徑或每次迭代調(diào)用的函數(shù)。
[args]
(... *):用來調(diào)用每個方法的參數(shù)。
(數(shù)組):返回結(jié)果數(shù)組。
_.invokeMap([[5, 1, 7], [3, 2, 1]], 'sort'); // => [[1, 5, 7], [1, 2, 3]] _.invokeMap([123, 456], String.prototype.split, ''); // => [['1', '2', '3'], ['4', '5', '6']]
創(chuàng)建一個由運行集合中的每個元素到迭代器的結(jié)果生成的鍵組成的對象。 每個鍵的相應值是負責生成密鑰的最后一個元素。 迭代器被調(diào)用一個參數(shù):(value)。
4.0.0
collection
(Array | Object):迭代的集合。
[iteratee=_.identity]
(函數(shù)):用于轉(zhuǎn)換密鑰的迭代器。
(Object):返回組合的聚合對象。
var array = [ { 'dir': 'left', 'code': 97 }, { 'dir': 'right', 'code': 100 }]; _.keyBy(array, function(o) { return String.fromCharCode(o.code);}); // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } } _.keyBy(array, 'dir'); // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }
通過運行iteratee中的每個元素來創(chuàng)建一個值的數(shù)組。 迭代器被調(diào)用三個參數(shù):
(value, index|key, collection).
可以用許多l(xiāng)odash方法防護,iteratees工作如下:_.every
,_.filter
, _.map
,_.mapValues
,_.reject
,和_.some
。
防護的方法是:
ary
, chunk
, curry
, curryRight
, drop
, dropRight
, every
, fill
, invert
, parseInt
, random
, range
, rangeRight
, repeat
, sampleSize
, slice
, some
, sortBy
, split
, take
, takeRight
, template
, trim
, trimEnd
, trimStart
, and words
0.1.0
collection
(Array | Object):迭代的集合。
[iteratee=_.identity]
(Function):每次迭代調(diào)用的函數(shù)。
(Array):返回新的映射數(shù)組。
function square(n) { return n * n;} _.map([4, 8], square); // => [16, 64] _.map({ 'a': 4, 'b': 8 }, square); // => [16, 64] (iteration order is not guaranteed) var users = [ { 'user': 'barney' }, { 'user': 'fred' }]; // The `_.property` iteratee shorthand. _.map(users, 'user'); // => ['barney', 'fred']
此方法與_.sortBy類似,只是它允許指定要排序的迭代的排序順序。 如果訂單未指定,則所有值均按升序排序。 否則,請指定“desc”的降序順序或“asc”順序的升序順序。
4.0.0
collection
(Array | Object):迭代的集合。
[iteratees=[_.identity]]
(Array [] | Function [] | Object [] | string []):要進行排序的迭代。
[orders]
(string []):的排序順序iteratees
。
(Array):返回新的排序數(shù)組。
var users = [ { 'user': 'fred', 'age': 48 }, { 'user': 'barney', 'age': 34 }, { 'user': 'fred', 'age': 40 }, { 'user': 'barney', 'age': 36 }]; // Sort by `user` in ascending order and by `age` in descending order. _.orderBy(users, ['user', 'age'], ['asc', 'desc']); // => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]
創(chuàng)建一個分成兩組的元素數(shù)組,其中第一個包含謂詞返回truthy for的元素,第二個包含predicate返回falsey的元素。 謂詞用一個參數(shù)調(diào)用:(value)。
3.0.0
collection
(Array | Object):迭代的集合。
[predicate=_.identity]
(Function):每次迭代調(diào)用的函數(shù)。
(Array):返回分組元素的數(shù)組。
var users = [ { 'user': 'barney', 'age': 36, 'active': false }, { 'user': 'fred', 'age': 40, 'active': true }, { 'user': 'pebbles', 'age': 1, 'active': false }]; _.partition(users, function(o) { return o.active; }); // => objects for [['fred'], ['barney', 'pebbles']] // The `_.matches` iteratee shorthand. _.partition(users, { 'age': 1, 'active': false }); // => objects for [['pebbles'], ['barney', 'fred']] // The `_.matchesProperty` iteratee shorthand. _.partition(users, ['active', false]); // => objects for [['barney', 'pebbles'], ['fred']] // The `_.property` iteratee shorthand. _.partition(users, 'active'); // => objects for [['fred'], ['barney', 'pebbles']]
將集合減少到一個值,該值是通過迭代器運行collection中的每個元素的累積結(jié)果,其中每個連續(xù)的調(diào)用都提供了前一個的返回值。 如果沒有累加器,則采集的第一個元素被用作初始值。 迭代器被調(diào)用四個參數(shù):
(accumulator, value, index|key, collection).
有許多l(xiāng)odash方法防護,iteratees的方法如_.reduce
,_.reduceRight
和_.transform
。
防護的方法是:
assign
, defaults
, defaultsDeep
, includes
, merge
, orderBy
, and sortBy
0.1.0
collection
(Array | Object):迭代的集合。
[iteratee=_.identity]
(Function):每次迭代調(diào)用的函數(shù)。
[accumulator]
(*):初始值。
(*):返回累計值。
_.reduce([1, 2], function(sum, n) { return sum + n;}, 0);// => 3 _.reduce({ 'a': 1, 'b': 2, 'c': 1 }, function(result, value, key) { (result[value] || (result[value] = [])).push(key); return result;}, {}); // => { '1': ['a', 'c'], '2': ['b'] } (iteration order is not guaranteed)
這個方法就像_.reduce
,不同的是它遍歷collection
從右到左的元素。
0.1.0
collection
(Array | Object):迭代的集合。
[iteratee=_.identity]
(Function):每次迭代調(diào)用的函數(shù)。
[accumulator]
(*):初始值。
(*):返回累計值。
var array = [[0, 1], [2, 3], [4, 5]]; _.reduceRight(array, function(flattened, other) { return flattened.concat(other);}, []); // => [4, 5, 2, 3, 0, 1]
與_.filter相反; 此方法返回謂詞不返回真值的集合元素。
0.1.0
collection
(Array | Object):迭代的集合。
[predicate=_.identity]
(Function):每次迭代調(diào)用的函數(shù)。
(數(shù)組):返回新的過濾數(shù)組。
var users = [ { 'user': 'barney', 'age': 36, 'active': false }, { 'user': 'fred', 'age': 40, 'active': true }]; _.reject(users, function(o) { return !o.active; }); // => objects for ['fred'] // The `_.matches` iteratee shorthand. _.reject(users, { 'age': 40, 'active': true }); // => objects for ['barney'] // The `_.matchesProperty` iteratee shorthand. _.reject(users, ['active', false]); // => objects for ['fred'] // The `_.property` iteratee shorthand. _.reject(users, 'active'); // => objects for ['barney']
從collection
中取一個隨機元素。
2.0.0
collection
(Array | Object):要采樣的集合。
(*):返回隨機元素。
_.sample([1, 2, 3, 4]);// => 2
獲取集合中唯一鍵的n個隨機元素(大小不等)
4.0.0
collection
(Array | Object):要采樣的集合。
[n=1]
(數(shù)字):要采樣的元素數(shù)量。
(數(shù)組):返回隨機元素。
_.sampleSize([1, 2, 3], 2);// => [3, 1] _.sampleSize([1, 2, 3], 4);// => [2, 3, 1]
使用Fisher-Yates shuffle版本創(chuàng)建一個混合值數(shù)組。
0.1.0
collection
(Array | Object):要刷新的集合。
(數(shù)組):返回新的混洗數(shù)組。
_.shuffle([1, 2, 3, 4]);// => [4, 1, 3, 2]
通過返回對象的數(shù)組類型值的長度或自己的可枚舉字符串鍵控屬性的數(shù)量來獲取集合的大小。
0.1.0
collection
(Array | Object | string):要檢查的集合。
(數(shù)字):返回集合大小。
_.size([1, 2, 3]);// => 3 _.size({ 'a': 1, 'b': 2 });// => 2 _.size('pebbles');// => 7
檢查predicate
是否返回任何collection
的元素。 一旦謂詞返回真,迭代就會停止。 謂詞用三個參數(shù)調(diào)用:(value,index | key,collection)。
0.1.0
collection
(Array | Object):迭代的集合。
[predicate=_.identity]
(Function):每次迭代調(diào)用的函數(shù)。
(boolean):若有元素通過謂詞檢查,返回true,
否則false
。
_.some([null, 0, 'yes', false], Boolean); // => true var users = [ { 'user': 'barney', 'active': true }, { 'user': 'fred', 'active': false }]; // The `_.matches` iteratee shorthand. _.some(users, { 'user': 'barney', 'active': false }); // => false // The `_.matchesProperty` iteratee shorthand. _.some(users, ['active', false]); // => true // The `_.property` iteratee shorthand. _.some(users, 'active'); // => true
創(chuàng)建一個元素數(shù)組,按照每個迭代器運行集合中每個元素的結(jié)果按升序排序。此方法執(zhí)行穩(wěn)定的排序,即保留相同元素的原始排序順序。迭代被調(diào)用一個參數(shù):(value)。
0.1.0
collection
(Array | Object):迭代的集合。
[iteratees=[_.identity]]
(...(Function | Function [])):按迭代排序。
(Array):返回新的排序數(shù)組。
var users = [ { 'user': 'fred', 'age': 48 }, { 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }, { 'user': 'barney', 'age': 34 }]; _.sortBy(users, [function(o) { return o.user; }]);// => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]] _.sortBy(users, ['user', 'age']);// => objects for [['barney', 34], ['barney', 36], ['fred', 40], ['fred', 48]]