Module: collection
集合相关函数,实现所有集合类数据结构的操作方法,比如array / arraylike / set / map / object / ...
Functions
countBy
▸ countBy(collection
, itee?
): Record
<string
, number
>
创建一个统计对象,对象的key是iteratee返回的值,对应的值是相同key出现的次数
Example
//{true: 5, false: 4}console.log(_.countBy([1,'a',3,'b',5,'c',7,'d',9],_.isNumber))const users = [ {name:'zhangsan',sex:'m',age:33}, {name:'lisi',sex:'f',age:21}, {name:'wangwu',sex:'m',age:25}, {name:'zhaoliu',sex:'m',age:44},]//{m: 3, f: 1} 性别分布统计console.log(_.countBy(users,u=>u.sex))//{20: 2, 30: 1, 40: 1} 年龄段分布统计console.log(_.countBy(users,u=>(u.age/10>>0)*10))
Since
1.0.0
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <unknown > | 任何可遍历的集合类型,比如array / arraylike / set / map / object / ... |
itee? | NonFuncItee | (value : unknown ) => string | - |
Returns
Record
<string
, number
>
统计对象
▸ countBy<K
>(collection
, itee?
): Record
<K
, number
>
Type parameters
Name | Type |
---|---|
K | extends string | number | symbol |
Parameters
Name | Type |
---|---|
collection | Collection <unknown > |
itee? | NonFuncItee | (value : unknown ) => K |
Returns
Record
<K
, number
>
each
▸ each<V
>(collection
, callback
): void
对集合元素进行顺序遍历。 注意,object类型无法保证遍历顺序
Example
//1、2、3_.each(new Set([1,2,3]),console.log)//a、b、c_.each({'1':'a','2':'b','3':'c'},console.log)//1、{"a":1}、[2,3]_.each([1,{a:1},[2,3]],console.log)//h/o/l/y/h/i/g/h_.each('holyhigh',console.log)//遍历元素集合const x=[];_.each(document.body.children,v=>x.push(v));console.log(x)
Type parameters
Name |
---|
V |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <V > | 任何可遍历的集合类型,比如array / arraylike / set / map / object / ... |
callback | (value : V , index : UnknownMapKey , collection : Collection <V >) => boolean | void | (value,index|key,collection);回调函数,如果返回false会立即中断遍历 |
Returns
void
▸ each<V
, K
>(collection
, callback
): void
Type parameters
Name | Type |
---|---|
V | V |
K | extends string | number | symbol |
Parameters
Name | Type |
---|---|
collection | Collection <V > |
callback | (value : V , index : K , collection : Collection <V >) => boolean | void |
Returns
void
eachRight
▸ eachRight<V
>(collection
, callback
): void
对集合元素进行顺序遍历,与 forEach 不同在于遍历顺序是从右到左 注意,object类型无法保证遍历顺序
Example
//3、2、1_.eachRight(new Set([1,2,3]),console.log)//c、b、a_.eachRight({'1':'a','2':'b','3':'c'},console.log)//[2,3]、{"a":1}、1_.eachRight([1,{a:1},[2,3]],console.log)//hgihyloh_.eachRight('holyhigh',console.log)
Type parameters
Name |
---|
V |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <V > | 任何可遍历的集合类型,比如array / arraylike / set / map / object / ... |
callback | (value : V , index : UnknownMapKey , collection : Collection <V >) => boolean | void | (value,index|key,collection);回调函数,如果返回false会立即中断遍历 |
Returns
void
▸ eachRight<V
, K
>(collection
, callback
): void
Type parameters
Name | Type |
---|---|
V | V |
K | extends string | number | symbol |
Parameters
Name | Type |
---|---|
collection | Collection <V > |
callback | (value : V , index : K , collection : Collection <V >) => boolean | void |
Returns
void
every
▸ every<V
>(collection
, predicate
): boolean
对集合内的所有元素进行断言,直到第一个返回false的元素结束。如果所有元素断言都为真返回true
Example
const libs = [ {name:'func.js',platform:['web','nodejs'],tags:{utils:true},js:true}, {name:'juth2',platform:['web','java'],tags:{utils:false,middleware:true},js:false}, {name:'soya2d',platform:['web'],tags:{utils:true},js:true}];//trueconsole.log(_.every([]))//trueconsole.log(_.every([1,3,5],v=>v%2===1))//falseconsole.log(_.every(['a','b','c',1],_.isNumber))//falseconsole.log(_.every(libs,'tags.utils'))//falseconsole.log(_.every(libs,{js:true}))//false key不支持路径解析console.log(_.every(libs,{'platform[0]':'web'}))
Type parameters
Name |
---|
V |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <V > | 任何可遍历的集合类型,比如array / arraylike / set / map / object / ... |
predicate | NonFuncItee | (value : V , index : UnknownMapKey , collection : Collection <V >) => boolean | (value,index|key,collection) 断言 当断言是函数时回调参数见定义 其他类型请参考 iteratee |
Returns
boolean
全部通过返回true,否则false。对于一个空集合,会返回true
▸ every<V
, K
>(collection
, predicate
): boolean
Type parameters
Name | Type |
---|---|
V | V |
K | extends string | number | symbol |
Parameters
Name | Type |
---|---|
collection | Collection <V > |
predicate | NonFuncItee | (value : V , index : K , collection : Collection <V >) => boolean |
Returns
boolean
filter
▸ filter<V
>(collection
, predicate
): V
返回一个新数组,数组内容由集合内所有断言结果为真的元素组成
Example
const libs = [ {name:'func.js',platform:['web','nodejs'],tags:{utils:true},js:false}, {name:'juth2',platform:['web','java'],tags:{utils:false,middleware:true},js:true}, {name:'soya2d',platform:['web'],tags:{utils:true},js:true}];//[]console.log(_.filter())//[1,3]console.log(_.filter([1,2,3,4],v=>v%2===1))//[1]console.log(_.filter(['a','b','c',1],_.isNumber))//[1,2]console.log(_.filter({a:1,b:2,c:'3'},_.isNumber))//[f、s]console.log(_.filter(libs,'tags.utils'))//[j、s]console.log(_.filter(libs,{js:true}))//[] key不支持路径解析console.log(_.filter(libs,{'platform[0]':'web'}))
Type parameters
Name |
---|
V |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <V > | 任何可遍历的集合类型,比如array / arraylike / set / map / object / ... |
predicate | NonFuncItee | (value : V , index : UnknownMapKey , collection : Collection <V >) => boolean | (value,index|key,collection) 断言 当断言是函数时回调参数见定义 其他类型请参考 iteratee |
Returns
V
由通过断言的元素组成的新数组
▸ filter<V
, K
>(collection
, predicate
): V
Type parameters
Name | Type |
---|---|
V | V |
K | extends string | number | symbol |
Parameters
Name | Type |
---|---|
collection | Collection <V > |
predicate | NonFuncItee | (value : V , index : K , collection : Collection <V >) => boolean |
Returns
V
find
▸ find<V
>(collection
, predicate
): V
| undefined
对集合内的所有元素进行断言并返回第一个匹配的元素
Example
const libs = [ {name:'func.js',platform:['web','nodejs'],tags:{utils:true},js:false}, {name:'juth2',platform:['web','java'],tags:{utils:false,middleware:true},js:true}, {name:'soya2d',platform:['web'],tags:{utils:true},js:true}];//1console.log(_.find(['a','b','c',1,3,6],_.isNumber))//holyhighconsole.log(_.find({a:1,b:true,c:'holyhigh',d:'func.js'},_.isString))//{f}console.log(_.find(libs,'tags.utils'))//{j}console.log(_.find(libs,{js:true}))//undefined key不支持路径解析console.log(_.find(libs,{'tags.utils':false}))
Type parameters
Name |
---|
V |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <V > | 任何可遍历的集合类型,比如array / arraylike / set / map / object / ... |
predicate | NonFuncItee | (value : V , index : string | number | symbol , collection : Collection <V >) => boolean | (value,index|key,collection) 断言 当断言是函数时回调参数见定义 其他类型请参考 iteratee |
Returns
V
| undefined
第一个匹配断言的元素或undefined
▸ find<V
, K
>(collection
, predicate
): V
| undefined
Type parameters
Name | Type |
---|---|
V | V |
K | extends string | number | symbol |
Parameters
Name | Type |
---|---|
collection | Collection <V > |
predicate | NonFuncItee | (value : V , index : K , collection : Collection <V >) => boolean |
Returns
V
| undefined
findLast
▸ findLast<V
>(collection
, predicate
): V
| undefined
对集合内的所有元素进行断言并返回最后一个匹配的元素
Example
const libs = [ {name:'func.js',platform:['web','nodejs'],tags:{utils:false},js:false}, {name:'juth2',platform:['web','java'],tags:{utils:false,middleware:true},js:true}, {name:'soya2d',platform:['web'],tags:{utils:true},js:true}];//6console.log(_.findLast(['a','b','c',1,3,6],_.isNumber))//func.jsconsole.log(_.findLast({a:1,b:true,c:'holyhigh',d:'func.js'},_.isString))//{s}console.log(_.findLast(libs,'tags.utils'))//{s}console.log(_.findLast(libs,{js:true}))//undefined key不支持路径解析console.log(_.findLast(libs,{'tags.utils':false}))
Type parameters
Name |
---|
V |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <V > | 任何可遍历的集合类型,比如array / arraylike / set / map / object / ... |
predicate | NonFuncItee | (value : V , index : string | number | symbol , collection : Collection <V >) => boolean | (value,index|key,collection) 断言 当断言是函数时回调参数见定义 其他类型请参考 iteratee |
Returns
V
| undefined
第一个匹配断言的元素或undefined
▸ findLast<V
, K
>(collection
, predicate
): V
| undefined
Type parameters
Name | Type |
---|---|
V | V |
K | extends string | number | symbol |
Parameters
Name | Type |
---|---|
collection | Collection <V > |
predicate | NonFuncItee | (value : V , index : K , collection : Collection <V >) => boolean |
Returns
V
| undefined
flatMap
▸ flatMap<V
>(collection
, itee
, depth?
): V
类似map
,但会对返回值进行flat
处理。
除此之外,与map函数最大的不同在于返回值与元素的映射关系并不一定是一一对应,此时更像filter
Example
//[1, 2, [3]]console.log(_.flatMap([[1,2],[[3]]]))//[3,5]console.log(_.flatMap([[1,2],3,4,5],n=>n%2?n:[]))
Since
1.0.0
Type parameters
Name |
---|
V |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <V > | 任何可遍历的集合类型,比如array / arraylike / set / map / object / ... |
itee | NonFuncItee | (value : V , index : string | number | symbol , collection : Collection <V >) => V | - |
depth? | number | 嵌套深度 |
Returns
V
映射值的新数组
▸ flatMap<V
, K
>(collection
, itee
, depth?
): V
Type parameters
Name | Type |
---|---|
V | V |
K | extends string | number | symbol |
Parameters
Name | Type |
---|---|
collection | Collection <V > |
itee | NonFuncItee | (value : V , index : K , collection : Collection <V >) => V |
depth? | number |
Returns
V
▸ flatMap<V
, K
, U
>(collection
, itee
, depth?
): U
Type parameters
Name | Type |
---|---|
V | V |
K | extends string | number | symbol |
U | U |
Parameters
Name | Type |
---|---|
collection | Collection <V > |
itee | NonFuncItee | (value : V , index : K , collection : Collection <V >) => U |
depth? | number |
Returns
U
flatMapDeep
▸ flatMapDeep<V
>(collection
, itee
): V
同flatMap
,但会递归元素进行扁平化处理
Example
//[1, 2, 3]console.log(_.flatMapDeep([[1,2],[[3]]]))
Since
1.0.0
Type parameters
Name |
---|
V |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <V > | 任何可遍历的集合类型,比如array / arraylike / set / map / object / ... |
itee | NonFuncItee | (value : V , index : string | number | symbol , collection : Collection <V >) => V | - |
Returns
V
映射值的新数组
▸ flatMapDeep<V
, K
>(collection
, itee
): V
Type parameters
Name | Type |
---|---|
V | V |
K | extends string | number | symbol |
Parameters
Name | Type |
---|---|
collection | Collection <V > |
itee | NonFuncItee | (value : V , index : K , collection : Collection <V >) => V |
Returns
V
▸ flatMapDeep<V
, K
, U
>(collection
, itee
): U
Type parameters
Name | Type |
---|---|
V | V |
K | extends string | number | symbol |
U | U |
Parameters
Name | Type |
---|---|
collection | Collection <V > |
itee | NonFuncItee | (value : V , index : K , collection : Collection <V >) => U |
Returns
U
groupBy
▸ groupBy(collection
, itee?
): Record
<UnknownMapKey
, unknown
>
创建一个统计对象,对象的key是iteratee返回的值,对应的值是由所有key对应值组成的数组
Example
//{true: [1, 3, 5, 7, 9], false: ['a', 'b', 'c', 'd']}console.log(_.groupBy([1,'a',3,'b',5,'c',7,'d',9],_.isNumber))const users = [ {name:'zhangsan',sex:'m',age:33}, {name:'lisi',sex:'f',age:21}, {name:'wangwu',sex:'m',age:25}, {name:'zhaoliu',sex:'m',age:44},]//{m: [{...},{...},{...}], f: [{...}]} 性别分布统计console.log(_.groupBy(users,u=>u.sex))//{20: [{...},{...}], 30: [{...}], 40: [{...}]} 年龄段分布统计console.log(_.groupBy(users,u=>(u.age/10>>0)*10))
Since
1.0.0
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <unknown > | 任何可遍历的集合类型,比如array / arraylike / set / map / object / ... |
itee? | NonFuncItee | (value : unknown ) => UnknownMapKey | - |
Returns
Record
<UnknownMapKey
, unknown
>
统计对象
includes
▸ includes(collection
, value
, fromIndex?
): boolean
判断集合中是否包含给定的值。使用eq
函数进行等值判断。
Example
//trueconsole.log(_.includes({a:1,b:2},2))//falseconsole.log(_.includes([1,3,5,7,[2]],2))//trueconsole.log(_.includes([1,3,5,7,[2]],3))//falseconsole.log(_.includes([1,3,5,7,[2]],3,2))//trueconsole.log(_.includes([0,null,undefined,NaN],NaN))//trueconsole.log(_.includes('abcdefg','abc'))//falseconsole.log(_.includes('abcdefg','abc',2))//falseconsole.log(_.includes('aBcDeFg','abc'))
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <unknown > | 如果集合是map/object对象,则只对value进行比对 |
value | any | |
fromIndex? | number | 从集合的fromIndex 索引处开始查找。如果集合是map/object对象,无效 |
Returns
boolean
如果包含返回true否则返回false
keyBy
▸ keyBy<K
>(collection
, itee?
): Record
<K
, unknown
>
创建一个对象,对象的key是iteratee返回的值,对象的值是collection中最后一个key对应的值
Example
//{true: 9, false: 'd'}console.log(_.keyBy([1,'a',3,'b',5,'c',7,'d',9],_.isNumber))gconst users = [ {name:'zhangsan',sex:'m',age:33}, {name:'lisi',sex:'f',age:21}, {name:'wangwu',sex:'m',age:25}, {name:'zhaoliu',sex:'m',age:44},]//{m: {...}, f: {...}}console.log(_.keyBy(users,u=>u.sex))//{20: {...}, 30: {...}, 40: {...} }console.log(_.keyBy(users,u=>(u.age/10>>0)*10))
Since
1.0.0
Type parameters
Name | Type |
---|---|
K | extends string | number | symbol |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <unknown > | 任何可遍历的集合类型,比如array / arraylike / set / map / object / ... |
itee? | NonFuncItee | (value : unknown ) => K | - |
Returns
Record
<K
, unknown
>
统计对象
▸ keyBy<K
, V
>(collection
, itee?
): Record
<K
, V
>
Type parameters
Name | Type |
---|---|
K | extends string | number | symbol |
V | V |
Parameters
Name | Type |
---|---|
collection | Collection <unknown > |
itee? | NonFuncItee | (value : V ) => K |
Returns
Record
<K
, V
>
map
▸ map<V
>(collection
, itee
): V
返回一个新数组,该数组中的每个元素是调用一次callback函数后的返回值
Example
const libs = [ {name:'func.js',platform:['web','nodejs'],tags:{utils:true},js:false}, {name:'juth2',platform:['web','java'],tags:{utils:false,middleware:true},js:true}, {name:'soya2d',platform:['web'],tags:{utils:true},js:true}];//[2,4,6]console.log(_.map(new Set([1,2,3]),v => v*2))//[1,2,3]console.log(_.map({'1':'a','2':'b','3':'c'},(v,k)=>k))//[true,false,false]console.log(_.map([1,{a:1},[2,3]],v => _.isNumber(v)))//["H", "O", "L", "Y", "H", "I", "G", "H"]console.log(_.map('holyhigh',v => String.fromCharCode(v.charCodeAt(0)-32)))//[true,false,true]console.log(_.map(libs,'tags.utils'))//["func.js", "juth2", "soya2d"]console.log(_.map(libs,'name'))//[1,2,3]console.log(_.map({a:1,b:2,c:3}))
Type parameters
Name |
---|
V |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <V > | 任何可遍历的集合类型,比如array / arraylike / set / map / object / ... |
itee | NonFuncItee | (value : V , index : UnknownMapKey , collection : Collection <V >) => V | - |
Returns
V
映射值的新数组
▸ map<V
, K
>(collection
, itee
): V
Type parameters
Name | Type |
---|---|
V | V |
K | extends string | number | symbol |
Parameters
Name | Type |
---|---|
collection | Collection <V > |
itee | NonFuncItee | (value : V , index : K , collection : Collection <V >) => V |
Returns
V
▸ map<V
, K
, U
>(collection
, itee
): U
Type parameters
Name | Type |
---|---|
V | V |
K | extends string | number | symbol |
U | U |
Parameters
Name | Type |
---|---|
collection | Collection <V > |
itee | NonFuncItee | (value : V , index : K , collection : Collection <V >) => U |
Returns
U
partition
▸ partition<V
>(collection
, predicate
): V
[]
类似filter
函数,但返回固定长度为2的二维数组 - [[matched...],mismatched...]
Example
const libs = [ {name:'func.js',platform:['web','nodejs'],tags:{utils:true},js:false}, {name:'juth2',platform:['web','java'],tags:{utils:false,middleware:true},js:true}, {name:'soya2d',platform:['web'],tags:{utils:true},js:true}];//[[func.js],[juth2,soya2d]]console.log(_.partition(libs,{name:'func.js'}))const seq = [1,2,3,4,5,6];//[[2, 4, 6],[1, 3, 5]]console.log(_.partition(seq,n=>n%2===0))//[[1,3],["2"]]console.log(_.partition({a:1,b:'2',c:3},_.isNumber))
Since
0.17.0
Type parameters
Name |
---|
V |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <V > | 任何可遍历的集合类型,比如array / arraylike / set / map / object / ... |
predicate | NonFuncItee | (value : V , index : string | number | symbol , collection : Collection <V >) => boolean | (value,index|key,collection) 断言 当断言是函数时回调参数见定义 其他类型请参考 iteratee |
Returns
V
[]
由匹配列表,非匹配列表构成的二维数组
▸ partition<V
, K
>(collection
, predicate
): V
[]
Type parameters
Name | Type |
---|---|
V | V |
K | extends string | number | symbol |
Parameters
Name | Type |
---|---|
collection | Collection <V > |
predicate | NonFuncItee | (value : V , index : K , collection : Collection <V >) => boolean |
Returns
V
[]
reduce
▸ reduce<T
>(collection
, callback
, initialValue?
): T
对集合中的每个元素执行一次reducer函数,并将其结果汇总为单个值返回。
如果没有提供initialValue,reduce 会从集合索引1开始执行 callback 方法。如果提供initialValue则从索引0开始。
注意,对于Object类型的对象,如果未提供initialValue,则accumulator会是索引0元素的value,而不是key
Example
//25console.log(_.reduce([1,3,5,7,9],(a,v)=>a+v))//35console.log(_.reduce([1,3,5,7,9],(a,v)=>a+v,10))//x-y-zconsole.log(_.reduce({x:1,y:2,z:3},(a,v,k)=>a+'-'+k,'').substr(1))
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <T > | |
callback | (accumulator : T , value : T , key : UnknownMapKey , collection : Collection <T >) => T | (accumulator,value,key|index,collection);reducer函数 |
initialValue? | T | 第一次调用 callback函数时的第一个参数的值 |
Returns
T
汇总值
reject
▸ reject<V
>(collection
, predicate
): V
filter
的反函数,数组内容由集合内所有断言结果为假的元素组成
Example
//['a', 'b', 'c']console.log(_.reject(['a','b','c',1],_.isNumber))//['3']console.log(_.reject({a:1,b:2,c:'3'},_.isNumber))//[2,4]console.log(_.reject([1,2,3,4],v=>v%2===1))
Since
1.0.0
Type parameters
Name |
---|
V |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <V > | 任何可遍历的集合类型,比如array / arraylike / set / map / object / ... |
predicate | NonFuncItee | (value : V , index : UnknownMapKey , collection : Collection <V >) => boolean | (value,index|key,collection) 断言 当断言是函数时回调参数见定义 其他类型请参考 iteratee |
Returns
V
由通过断言的元素组成的新数组
▸ reject<V
, K
>(collection
, predicate
): V
Type parameters
Name | Type |
---|---|
V | V |
K | extends string | number | symbol |
Parameters
Name | Type |
---|---|
collection | Collection <V > |
predicate | NonFuncItee | (value : V , index : K , collection : Collection <V >) => boolean |
Returns
V
sample
▸ sample<T
>(collection
): T
返回对指定列表的唯一随机采样结果
Example
//随机值console.log(_.sample([1,2,3,4,5,6,7,8,9,0]))//随机值console.log(_.sample({a:1,b:2,c:3,d:4,e:5}))
Since
0.16.0
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <T > | 任何可遍历的集合类型,比如array / arraylike / set / map / object / ... |
Returns
T
采样结果
sampleSize
▸ sampleSize<T
>(collection
, count?
): T
返回对指定列表的指定数量随机采样结果
Example
//[随机值]console.log(_.sampleSize([1,2,3,4,5,6,7,8,9,0]))//[随机值1,随机值2]console.log(_.sampleSize([{a:1},{b:2},{c:3},{d:4},{e:5}],2))//[随机值1,随机值2,随机值3]console.log(_.sampleSize({a:1,b:2,c:3,d:4,e:5},3))
Since
0.16.0
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <T > | 任何可遍历的集合类型,比如array / arraylike / set / map / object / ... |
count? | number | 采样数量 |
Returns
T
采样结果
shuffle
▸ shuffle<T
>(collection
): T
返回指定数组的一个随机乱序副本
Example
//[随机内容]console.log(_.shuffle([1,2,3,4,5,6,7,8,9,0]))//[随机内容]console.log(_.shuffle([{a:1},{a:2},{a:3},{a:4},{a:5}]))//[随机内容]console.log(_.shuffle({a:1,b:2,c:3,d:4,e:5}))
Since
0.16.0
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <T > | 任何可遍历的集合类型,比如array / arraylike / set / map / object / ... |
Returns
T
乱序副本
size
▸ size(collection
): number
获取集合对象的内容数量,对于map/object对象获取的是键/值对的数量
Example
//3console.log(_.size({a:1,b:2,c:{x:1}}))//0console.log(_.size(null))//3console.log(_.size(new Set([1,2,3])))//2console.log(_.size([1,[2,[3]]]))//2console.log(_.size(document.body.children))//4console.log(_.size(document.body.childNodes))//3 arguments已不推荐使用,请使用Rest参数console.log((function(){return _.size(arguments)})('a',2,'b'))//7console.log(_.size('func.js'))
Parameters
Name | Type |
---|---|
collection | Collection <unknown > |
Returns
number
集合长度,对于null/undefined/WeakMap/WeakSet返回0
some
▸ some<V
>(collection
, predicate
): boolean
对集合内的所有元素进行断言,直到第一个返回true的元素结束。
Example
const libs = [ {name:'func.js',platform:['web','nodejs'],tags:{utils:true},js:true}, {name:'juth2',platform:['web','java'],tags:{utils:false,middleware:true},js:false}, {name:'soya2d',platform:['web'],tags:{utils:true},js:true}];//falseconsole.log(_.some([]))//trueconsole.log(_.some([1,2,3,4],v=>v%2===1))//trueconsole.log(_.some(['a','b','c',1],_.isNumber))//trueconsole.log(_.some(libs,'tags.middleware'))//trueconsole.log(_.some(libs,{js:true}))//false key不支持路径解析console.log(_.some(libs,{'tags.utils':false}))
Type parameters
Name |
---|
V |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <V > | 任何可遍历的集合类型,比如arraylike / set / map / object / ... |
predicate | NonFuncItee | (value : V , index : UnknownMapKey , collection : Collection <V >) => boolean | (value,index|key,collection) 断言 当断言是函数时回调参数见定义 其他类型请参考 iteratee |
Returns
boolean
只要有任意元素断言为真返回true,否则false。对于一个空集合,会返回false
▸ some<V
, K
>(collection
, predicate
): boolean
Type parameters
Name | Type |
---|---|
V | V |
K | extends string | number | symbol |
Parameters
Name | Type |
---|---|
collection | Collection <V > |
predicate | NonFuncItee | (value : V , index : K , collection : Collection <V >) => boolean |
Returns
boolean
sort
▸ sort<T
>(collection
, comparator?
): T
对集合进行排序,并返回排序后的数组副本。
Example
//字符排序 ['lao1', 'lao2', 'lao3']console.log(_.sort(['lao1','lao3','lao2']))//数字排序[7, 9, 80]console.log(_.sort([9,80,7]))//日期排序["3/1/2019", "2020/1/1", Wed Apr 01 2020...]console.log(_.sort([new Date(2020,3,1),'2020/1/1','3/1/2019']))//第一个元素不是日期对象,需要转换console.log(_.sort(_.map(['2020/1/1',new Date(2020,3,1),'3/1/2019'],v=>new Date(v))))//对象排序const users = [ {name:'zhangsan',age:53}, {name:'lisi',age:44}, {name:'wangwu',age:25}, {name:'zhaoliu',age:36}];//[25,36,44,53]console.log(_.sort(users,(a,b)=>a.age-b.age))// 倒排console.log(_.sort(users,(a,b)=>b.age-a.age))
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <T > | 任何可遍历的集合类型,比如array / arraylike / set / map / object / ... |
comparator? | (a : T , b : T ) => number | (a,b) 排序函数,如果为空使用sortBy逻辑 |
Returns
T
排序后的数组
sortBy
▸ sortBy<V
>(collection
, itee?
): V
使用指定回调对集合结果进行升序排序。根据集合结果的第一个元素确定排序逻辑,内置排序逻辑包括
- 字符串
- 数字
- 日期
Example
//不变console.log(_.sortBy([{a:2},{a:1},{a:3}]))//[{{a:1},{a:2},{a:3}] 通过iteratee把集合变为数字后排序console.log(_.sortBy([{a:2},{a:1},{a:3}],'a'))//['3/1/2019', '2020/1/1', '2020-3-1']console.log(_.sortBy(['2020-3-1','2020/1/1','3/1/2019'],_.toDate))
Since
1.0.0
Type parameters
Name |
---|
V |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <V > | 任何可遍历的集合类型,比如array / arraylike / set / map / object / ... |
itee? | NonFuncItee | (value : V , index : string | number | symbol ) => any | - |
Returns
V
排序后的数组
▸ sortBy<V
, K
>(collection
, itee?
): V
Type parameters
Name | Type |
---|---|
V | V |
K | extends string | number | symbol |
Parameters
Name | Type |
---|---|
collection | Collection <V > |
itee? | NonFuncItee | (value : V , index : K ) => any |
Returns
V
toArray
▸ toArray<T
>(collection
): T
把一个集合对象转为array对象。对于非集合对象,
- 字符串 - 每个字符都会变成数组的元素
- 其他情况 - 返回包含一个collection元素的数组
Example
//[1,2,3]console.log(_.toArray(new Set([1,2,3])))//['a','b','c']console.log(_.toArray('abc'))//[1,2,'b']console.log(_.toArray({x:1,y:2,z:'b'}))//[[1, 'a'], [3, 'b'], ['a', 5]]console.log(_.toArray(new Map([[1,'a'],[3,'b'],['a',5]])))
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection <T > | 如果是Map/Object对象会转换为值列表 |
Returns
T
转换后的数组对象