Lodash教程--(3)“集合”方法 (“Collection” Methods)
(1)_.countBy(collection, [iteratee= _.identity])
(2)_.every(collection, [predicate= _.identity])
(3)_.filter(collection, [predicate= _.identity])
(4)_.find(collection, [predicate= _.identity], [fromIndex=0])
(5)_.findLast(collection, [predicate= _.identity], [fromIndex=collection.length-1])
(6)_.flatMap(collection, [iteratee= _.identity])
(7)_.flatMapDeep(collection, [iteratee= _.identity])
(8)_.flatMapDepth(collection, [iteratee= _.identity], [depth=1])
(9)_.forEach(collection, [iteratee= _.identity])
(10)_.forEachRight(collection, [iteratee= _.identity])
(11)_.groupBy(collection, [iteratee= _.identity])
(12) _.includes(collection, value, [fromIndex=0])
(13)_.invokeMap(collection, path, [args])
(14)_.keyBy(collection, [iteratee= _.identity])
(15)_.map(collection, [iteratee= _.identity])
(16)_.orderBy(collection, [iteratees=[ _.identity]], [orders])
(17)_.partition(collection, [predicate= _.identity])
(18)_.reduce(collection, [iteratee= _.identity], [accumulator])
(19)_.reduceRight(collection, [iteratee= _.identity], [accumulator])
(20)_.reject(collection, [predicate= _.identity])
(21) _.sample(collection)
(22)_.sampleSize(collection, [n=1])
(23)_.shuffle(collection)
(24)_.size(collection)
(25)_.some(collection, [predicate= _.identity])
(26)_.sortBy(collection, [iteratees=[ _.identity]])
(1)_.countBy(collection, [iteratee= _.identity])
创建一个组成对象,key
(键)是经过 iteratee
(迭代函数) 执行处理collection
中每个元素后返回的结果,每个key
(键)对应的值是 iteratee
(迭代函数)返回该key
(键)的次数(愚人码头注:迭代次数)。 iteratee
调用一个参数:(value
)。
collection (Array|Object)
: 一个用来迭代的集合。
[iteratee=_.identity] (Array|Function|Object|string)
: 一个迭代函数,用来转换key(键)。
_. countBy ( [ 6.1 , 4.2 , 6.3 ] , Math. floor) ;
_. countBy ( [ 'one' , 'two' , 'three' ] , 'length' ) ;
(2)_.every(collection, [predicate= _.identity])
通过 predicate
(断言函数) 检查 collection
(集合)中的 所有 元素是否都返回真值。一旦 predicate
(断言函数) 返回假值,迭代就马上停止。predicate
(断言函数)调用三个参数: (value, index|key, collection
)。
_. every ( [ true , 1 , null , 'yes' ] , Boolean) ;
var users = [
{
'user' : 'barney' , 'age' : 36 , 'active' : false } ,
{
'user' : 'fred' , 'age' : 40 , 'active' : false }
] ;
_. every ( users, {
'user' : 'barney' , 'active' : false } ) ;
_. every ( users, [ 'active' , false ] ) ;
_. every ( users, 'active' ) ;
(3)_.filter(collection, [predicate= _.identity])
遍历 collection
(集合)元素,返回 predicate
(断言函数)返回真值 的所有元素的数组。 predicate
(断言函数)调用三个参数:(value, index|key, collection
)。
var users = [
{
'user' : 'barney' , 'age' : 36 , 'active' : true } ,
{
'user' : 'fred' , 'age' : 40 , 'active' : false }
] ;
_. filter ( users, function ( o) {
return ! o. active; } ) ;
_. filter ( users, {
'age' : 36 , 'active' : true } ) ;
_. filter ( users, [ 'active' , false ] ) ;
_. filter ( users, 'active' ) ;
(4)_.find(collection, [predicate= _.identity], [fromIndex=0])
遍历 collection
(集合)元素,返回 predicate
(断言函数)第一个返回真值的第一个元素。predicate
(断言函数)调用3个参数: (value, index|key, collection
)。
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 ; } ) ;
_. find ( users, {
'age' : 1 , 'active' : true } ) ;
_. find ( users, [ 'active' ,