Module: utils/src/modules/__indexCollections
Functions
contains
▸ contains(list
, value?
, fromIndex?
): boolean
返回一个布尔值,判断给定集合是否包含指定的value
(使用===检测)。
Example
contains([1, 2, 3], 3)
=> true
contains({ a: 1, b: 2, c: 3 }, 3)
=> true
contains([1, 2, 3], 3)
=> true
contains({ a: 1, b: 2, c: 3 }, 3)
=> true
Parameters
Name | Type | Description |
---|---|---|
list | Collection | 给定集合 |
value? | unknown | 给定值 |
fromIndex? | number | boolean | 起始索引,传递 true 将采用二分查找(需数据已升序) |
Returns
boolean
countBy
▸ countBy<V
, I
>(list
, iteratee?
, context?
): Object
返回一个对象,对象由给定集合通过iterator
处理分组为多个计数后组成,类似groupBy
,但是不是返回列表的值,而是返回在该组中值的计数。
Example
countBy([1, 2, 3, 4, 5], (val) => val % 2 === 0 ? 'even': 'odd')
=> { odd: 3, even: 2 }
countBy([1, 2, 3, 4, 5], (val) => val % 2 === 0 ? 'even': 'odd')
=> { odd: 3, even: 2 }
Type parameters
Name | Type |
---|---|
V | extends Collection |
I | extends Iteratee <V , unknown > |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
iteratee? | I | 迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为迭代器函数的执行上下文 this |
Returns
Object
every
▸ every<V
, I
>(list
, predicate?
, context?
): boolean
数组推荐原生:Array.prototype.every。 返回一个布尔值,判断给定集合内的所有元素是否都能通过predicate
真值检测。
Example
every({a: 2, b: 4, c: 5}, (val) => val % 2 === 0)
=> false
every({a: 2, b: 4, c: 5}, (val) => val % 2 === 0)
=> false
Type parameters
Name | Type |
---|---|
V | extends Collection |
I | extends Iteratee <V , boolean > |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
predicate? | I | 谓语迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为谓语迭代器函数的执行上下文 this |
Returns
boolean
filter
▸ filter<V
, I
>(list
, predicate?
, context?
): TypeOfCollection
<V
>[]
数组推荐原生:Array.prototype.filter。 返回一个数组,数组由给定集合中通过predicate
真值检测的元素组成。
Example
filter({ a: 1, b: 2, c: 3, d: 4 }, (val) => val % 2 === 0)
=> [2, 4]
filter({ a: 1, b: 2, c: 3, d: 4 }, (val) => val % 2 === 0)
=> [2, 4]
Type parameters
Name | Type |
---|---|
V | extends Collection |
I | extends Iteratee <V , unknown > |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
predicate? | I | 谓语迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为谓语迭代器函数的执行上下文 this |
Returns
TypeOfCollection
<V
>[]
find
▸ find<V
, I
>(list
, predicate?
, context?
): undefined
| TypeOfCollection
<V
>
数组推荐原生:Array.prototype.find。 返回给定集合中第一个通过predicate
真值检测的元素值,如果没有元素通过检测则返回undefined
。
Example
find({ a: 1, b: 2, c: 3, d: 4 }, (val) => val % 2 === 0)
=> 2
find({ a: 1, b: 2, c: 3, d: 4 }, (val) => val % 2 === 0)
=> 2
Type parameters
Name | Type |
---|---|
V | extends Collection |
I | extends Iteratee <V , boolean > |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
predicate? | I | 谓语迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为谓语迭代器函数的执行上下文 this |
Returns
undefined
| TypeOfCollection
<V
>
findWhere
▸ findWhere<V
>(list
, properties
): undefined
| TypeOfCollection
<V
, never
>
返回给定集合通过matches(properties)
真值检测(键-值对匹配)的第一个元素。 如果没有找到匹配的属性,或者list
为空,返回undefined
。
Example
findWhere([{ id: 1 }, { id: 2, data: 'data' }, { id: 3 }], { id: 2 })
=> { id: 2, data: 'data' }
findWhere([{ id: 1 }, { id: 2, data: 'data' }, { id: 3 }], { id: 2 })
=> { id: 2, data: 'data' }
Type parameters
Name | Type |
---|---|
V | extends Collection |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
properties | Partial <TypeOfCollection <V >> | 谓语对象 |
Returns
undefined
| TypeOfCollection
<V
, never
>
forEach
▸ forEach<V
>(list
, iteratee?
, context?
): V
数组推荐原生:Array.prototype.forEach。 返回给定集合,遍历给定集合为每个元素执行一次iteratee
函数。 每次调用iteratee
都会传递三个参数:(element, index, list)
,可以通过返回false
提前结束遍历。 函数能在数组、对象、类数组对象,比如arguments
,NodeList
和类似的数据类型上正常工作。 但是它通过鸭子类型工作,所以要避免传递带有一个数值类型length
属性的对象。
Example
forEach([1, 2, 3], alert);
=> alert 1 2 3
forEach({ one: 1, two: 2, three: 3 }, alert);
=> alert 1 2 3
forEach([1, 2, 3], alert);
=> alert 1 2 3
forEach({ one: 1, two: 2, three: 3 }, alert);
=> alert 1 2 3
Type parameters
Name | Type |
---|---|
V | extends Collection |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
iteratee? | CollectionIterator <TypeOfCollection <V >, boolean | void , Collection <TypeOfCollection <V >>> | 迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为迭代器函数的执行上下文 this |
Returns
V
groupBy
▸ groupBy<V
, I
>(list
, iteratee?
, context?
): Object
返回一个对象,对象由给定集合通过iterator
处理分组为多个集合后组成,如果iterator
是一个字符串而不是函数, 那么将使用iterator
作为各元素的属性名来进行对比分组。
Example
groupBy([1.3, 2.1, 2.4], function(num){ return Math.floor(num) })
=> {1: [1.3], 2: [2.1, 2.4]}
groupBy(['one', 'two', 'three'], 'length')
=> { 3: ['one', 'two'], 5: ['three'] }
groupBy([1.3, 2.1, 2.4], function(num){ return Math.floor(num) })
=> {1: [1.3], 2: [2.1, 2.4]}
groupBy(['one', 'two', 'three'], 'length')
=> { 3: ['one', 'two'], 5: ['three'] }
Type parameters
Name | Type |
---|---|
V | extends Collection |
I | extends Iteratee <V , unknown > |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
iteratee? | I | 迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为迭代器函数的执行上下文 this |
Returns
Object
indexBy
▸ indexBy<V
, I
>(list
, iteratee?
, context?
): Object
返回一个对象,对象由给定集合通过iterator
处理分组为多个集合后组成,如果iterator
是一个字符串而不是函数, 那么将使用iterator
作为各元素的属性名来对比进行分组。和groupBy
非常像,但是当你知道你的键是唯一的时候可以使用indexBy
。
Example
const stooges = [{ name: 'moe', age: 40 }, { name: 'larry', age: 50 }, { name: 'curly', age: 60 }]
indexBy(stooges, 'age')
=> {
"40": { name: 'moe', age: 40 },
"50": { name: 'larry', age: 50 },
"60": { name: 'curly', age: 60 }
}
const stooges = [{ name: 'moe', age: 40 }, { name: 'larry', age: 50 }, { name: 'curly', age: 60 }]
indexBy(stooges, 'age')
=> {
"40": { name: 'moe', age: 40 },
"50": { name: 'larry', age: 50 },
"60": { name: 'curly', age: 60 }
}
Type parameters
Name | Type |
---|---|
V | extends Collection |
I | extends Iteratee <V , unknown > |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
iteratee? | I | 迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为迭代器函数的执行上下文 this |
Returns
Object
invoke
▸ invoke(list
, method
, ...args
): any
[]
返回一个数组,数组由给定集合中每个元素执行method
的返回值组成,传递args
则作为method
调用时的参数。 method
传递数组情况下,最后一位为方法名,其余为检索路径。
Example
invoke([[5, 1, 7], [3, 2, 1]], Array.prototype.srot)
=> [[1, 5, 7], [1, 2, 3]]
invoke([[5, 1, 7], [3, 2, 1]], 'sort')
=> [[1, 5, 7], [1, 2, 3]]
invoke([[5, 1, 7], [3, 2, 1]], Array.prototype.srot)
=> [[1, 5, 7], [1, 2, 3]]
invoke([[5, 1, 7], [3, 2, 1]], 'sort')
=> [[1, 5, 7], [1, 2, 3]]
Parameters
Name | Type | Description |
---|---|---|
list | Collection | 给定集合 |
method | string | Func <any > | string [] | 函数 or '函数名' or ['函数调用路径', '函数名'] |
...args | unknown [] | 调用时传递的参数 |
Returns
any
[]
map
▸ map<V
, I
>(list
, iteratee?
, context?
): IterateeResult
<I
, TypeOfCollection
<V
>>[]
数组推荐原生:Array.prototype.map。 返回一个数组,数组由给定集合中每个元素调用一次iteratee
函数的返回值组成。
Example
map([1, 2, 3], function(num){ return num * 3 })
=> [3, 6, 9]
map({ one: 1, two: 2, three: 3 }, function(num, key){ return num * 3 })
=> [3, 6, 9]
map([[1, 2], [3, 4]], first)
=> [1, 3]
map([1, 2, 3], function(num){ return num * 3 })
=> [3, 6, 9]
map({ one: 1, two: 2, three: 3 }, function(num, key){ return num * 3 })
=> [3, 6, 9]
map([[1, 2], [3, 4]], first)
=> [1, 3]
Type parameters
Name | Type |
---|---|
V | extends Collection |
I | extends Iteratee <V , unknown > |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
iteratee? | I | 迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为迭代器函数的执行上下文 this |
Returns
IterateeResult
<I
, TypeOfCollection
<V
>>[]
max
▸ max<V
, I
>(list
, iteratee?
, context?
): number
| TypeOfCollection
<V
>
返回给定集合中的最大值。如果传递iteratee
参数,iteratee
将作为给定集合中值的排序依据。 如果给定集合为空,将返回-Infinity
。
Example
const stooges = [{ name: 'moe', age: 40 }, { name: 'larry', age: 50}, { name: 'curly', age: 60 }]
max(stooges, function(stooge){ return stooge.age })
=> { name: 'curly', age: 60 }
const stooges = [{ name: 'moe', age: 40 }, { name: 'larry', age: 50}, { name: 'curly', age: 60 }]
max(stooges, function(stooge){ return stooge.age })
=> { name: 'curly', age: 60 }
Type parameters
Name | Type |
---|---|
V | extends Collection |
I | extends Iteratee <V , unknown > |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
iteratee? | I | 迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为迭代器函数的执行上下文 this |
Returns
number
| TypeOfCollection
<V
>
min
▸ min<V
, I
>(list
, iteratee?
, context?
): number
| TypeOfCollection
<V
>
返回给定集合中的最小值。如果传递iteratee
参数,iteratee
将作为给定集合中值的排序依据。 如果给定集合为空,将返回Infinity
。
Example
const numbers = [10, 5, 100, 2, 1000]
min(numbers)
=> 2
const numbers = [10, 5, 100, 2, 1000]
min(numbers)
=> 2
Type parameters
Name | Type |
---|---|
V | extends Collection |
I | extends Iteratee <V , unknown > |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
iteratee? | I | 迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为迭代器函数的执行上下文 this |
Returns
number
| TypeOfCollection
<V
>
partition
▸ partition<V
, I
>(list
, iteratee?
, context?
): Object
返回一个数组,数组由给定集合通过iteratee
处理分组为两个数组后组成,满足iteratee
的元素放入第一个数组,不满足放入第二个数组。
Example
partition([0, 1, 2, 3, 4, 5], isOdd)
=> [[1, 3, 5], [0, 2, 4]]
partition([0, 1, 2, 3, 4, 5], isOdd)
=> [[1, 3, 5], [0, 2, 4]]
Type parameters
Name | Type |
---|---|
V | extends Collection |
I | extends Iteratee <V , unknown > |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
iteratee? | I | 迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为迭代器函数的执行上下文 this |
Returns
Object
pluck
▸ pluck<V
, K
>(list
, key
): PropertyTypeOrAny
<TypeOfCollection
<V
>, K
>[]
返回一个数组,数组由给定集合的萃取属性值组成,map
最常使用的用例模型的简化版本。
Example
const stooges = [{ name: 'moe', age: 40 }, { name: 'larry', age: 50 }, { name: 'curly', age: 60 }]
pluck(stooges, 'name')
=> ['moe', 'larry', 'curly']
const stooges = [{ name: 'moe', age: 40 }, { name: 'larry', age: 50 }, { name: 'curly', age: 60 }]
pluck(stooges, 'name')
=> ['moe', 'larry', 'curly']
Type parameters
Name | Type |
---|---|
V | extends Collection |
K | extends string | number |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
key | K | 萃取属性 |
Returns
PropertyTypeOrAny
<TypeOfCollection
<V
>, K
>[]
reduce
▸ reduce<V
, TResult
>(list
, iteratee
, memo?
): TResult
返回将给定集合中元素值归结的数值。memo
是reduce
函数的初始值,会被每一次成功调用iteratee
函数的返回值所取代。 如果没有传递memo
给reduce
函数,iteratee
不会被列表中的第一个元素调用。第一个元素将取代memo
参数传递给列表中下一个元素调用的iteratee
函数。
Example
reduce([1, 2, 3], function(memo, num) { return memo + num })
=> 6
reduce([1, 2, 3], function(memo, num) { return memo + num }, 0)
=> 6
reduce([1, 2, 3], function(memo, num) { return memo + num })
=> 6
reduce([1, 2, 3], function(memo, num) { return memo + num }, 0)
=> 6
Type parameters
Name | Type |
---|---|
V | extends Collection |
TResult | TypeOfCollection <V > |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
iteratee | MemoCollectionIterator <TypeOfCollection <V >, TResult , Collection <TypeOfCollection <V >>> | 迭代器函数,通过 iteratee 进行转换(memo, value, index or key, list) |
memo? | TResult | 初始值 |
Returns
TResult
reduceRight
▸ reduceRight<V
, TResult
>(list
, iteratee
, memo?
): TResult
反向迭代集合,返回将给定集合中元素值归结的数值。memo
是reduce
函数的初始值,会被每一次成功调用iteratee
函数的返回值所取代。 如果没有传递memo
给reduce
函数,iteratee
不会被列表中的第一个元素调用。第一个元素将取代memo
参数传递给列表中下一个元素调用的iteratee
函数。
Example
const list = [[0, 1], [2, 3], [4, 5]]
reduceRight(list, function(a, b) { return a.concat(b) }, [])
=> [4, 5, 2, 3, 0, 1]
const list = [[0, 1], [2, 3], [4, 5]]
reduceRight(list, function(a, b) { return a.concat(b) }, [])
=> [4, 5, 2, 3, 0, 1]
Type parameters
Name | Type |
---|---|
V | extends Collection |
TResult | TypeOfCollection <V > |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
iteratee | MemoCollectionIterator <TypeOfCollection <V >, TResult , Collection <TypeOfCollection <V >>> | 迭代器函数,通过 iteratee 进行转换,(memo, value, index or key, list) |
memo? | TResult | 初始值 |
Returns
TResult
reject
▸ reject<V
, I
>(list
, predicate?
, context?
): TypeOfCollection
<V
, never
>[]
返回一个数组,数组由给定集合中没有通过predicate
真值检测的元素组成,filter
的反函数。
Example
var odds = reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0 })
=> [1, 3, 5]
var odds = reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0 })
=> [1, 3, 5]
Type parameters
Name | Type |
---|---|
V | extends Collection |
I | extends Iteratee <V , boolean > |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
predicate? | I | 谓语迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为谓语迭代器函数的执行上下文 this |
Returns
TypeOfCollection
<V
, never
>[]
sample
▸ sample<V
>(list
, n?
): TypeOfCollection
<V
>[]
返回一个数组,数组由给定集合随机萃取的n
个元素组成。
Example
sample([1, 2, 3, 4, 5, 6])
=> 4
sample([1, 2, 3, 4, 5, 6], 3)
=> [1, 6, 2]
sample([1, 2, 3, 4, 5, 6])
=> 4
sample([1, 2, 3, 4, 5, 6], 3)
=> [1, 6, 2]
Type parameters
Name | Type |
---|---|
V | extends Collection |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
n? | number | 萃取数量 |
Returns
TypeOfCollection
<V
>[]
shuffle
▸ shuffle<V
>(list
): TypeOfCollection
<V
>
返回一个数组,数组由给定集合使用Fisher-Yates shuffle
处理后组成.
Example
shuffle([1, 2, 3, 4, 5, 6])
=> [4, 1, 6, 3, 5, 2]
shuffle([1, 2, 3, 4, 5, 6])
=> [4, 1, 6, 3, 5, 2]
Type parameters
Name | Type |
---|---|
V | extends Collection |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
Returns
TypeOfCollection
<V
>
size
▸ size(list
): number
返回给定集合的长度。
Example
size([1, 2, 3, 4, 5])
=> 5
size({ one: 1, two: 2, three: 3 })
=> 3
size([1, 2, 3, 4, 5])
=> 5
size({ one: 1, two: 2, three: 3 })
=> 3
Parameters
Name | Type | Description |
---|---|---|
list | Collection | 给定集合 |
Returns
number
some
▸ some<V
, I
>(list
, predicate?
, context?
): boolean
数组推荐原生:Array.prototype.some。 返回一个布尔值,判断给定集合内是否至少有一个元素能通过predicate
真值检测。
Example
some([null, 0, 'yes', false], isString)
=> true
some([0, false], isString)
=> false
some([null, 0, 'yes', false], isString)
=> true
some([0, false], isString)
=> false
Type parameters
Name | Type |
---|---|
V | extends Collection |
I | extends Iteratee <V , unknown > |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
predicate? | I | 谓语迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为谓语迭代器函数的执行上下文 this |
Returns
boolean
sortBy
▸ sortBy<V
, I
>(list
, sortType
, iteratee?
, context?
): TypeOfCollection
<V
>[]
返回一个数组,数组由给定集合元素通过排序后组成,如果传递iteratee
参数,iteratee
将作为给定集合中值的排序依据。排序迭代器也可以是属性名称的字符串(比如length
)。
Example
sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num) })
=> [5, 4, 6, 3, 1, 2]
const stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}]
sortBy(stooges, 'name')
=> [{name: 'curly', age: 60}, {name: 'larry', age: 50}, {name: 'moe', age: 40}]
sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num) })
=> [5, 4, 6, 3, 1, 2]
const stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}]
sortBy(stooges, 'name')
=> [{name: 'curly', age: 60}, {name: 'larry', age: 50}, {name: 'moe', age: 40}]
Type parameters
Name | Type |
---|---|
V | extends Collection |
I | extends Iteratee <V , unknown > |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
sortType | boolean | 指定升序 or 降序 true-升 false-降 |
iteratee? | I | 迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为迭代器函数的执行上下文 this |
Returns
TypeOfCollection
<V
>[]
where
▸ where<V
>(list
, properties
): TypeOfCollection
<V
, never
>[]
返回一个数组,数组由给定集合中通过matcher(attrs)
真值测试(键-值对匹配)的元素组成。
Example
where([{id: 1, sex: 2}], {id: 1})
=> [{id: 1}]
where([{id: 1, sex: 2}], {id: 1})
=> [{id: 1}]
Type parameters
Name | Type |
---|---|
V | extends Collection |
Parameters
Name | Type | Description |
---|---|---|
list | V | 给定集合 |
properties | Partial <TypeOfCollection <V >> | 谓语对象 |
Returns
TypeOfCollection
<V
, never
>[]