collection

Source: index.js:6

集合处理工具函数


Static Methods

countBy( collection: Array | Object, iteratee?: function | string | number | Array | Object, ): Object

按 iteratee 结果统计集合元素数量。

Parameters

  • collection (Array | Object) — 要遍历的集合
  • iteratee (function | string | number | Array | Object, optional) — 判断函数或简写

Returns

  • Object — 键为分组值、值为计数的对象

Throws

  • TypeError — 集合或 iteratee 类型不正确时抛出

every( collection: Array | Object, predicate?: function | string | number | Array | Object, ): boolean

判断集合中的所有元素是否满足条件。

Parameters

  • collection (Array | Object) — 要遍历的集合
  • predicate (function | string | number | Array | Object, optional) — 判断函数或简写

Returns

  • boolean — 所有元素满足条件时为 true

Throws

  • TypeError — 集合或 predicate 类型不正确时抛出

filter( collection: Array | Object, predicate?: function | string | number | Array | Object, ): Array

返回满足条件的集合元素。

Parameters

  • collection (Array | Object) — 要遍历的集合
  • predicate (function | string | number | Array | Object, optional) — 判断函数或简写

Returns

  • Array — 满足条件的元素数组

Throws

  • TypeError — 集合或 predicate 类型不正确时抛出

find( collection: Array | Object, predicate?: function | string | number | Array | Object, ): *

返回集合中第一个满足条件的元素。

Parameters

  • collection (Array | Object) — 要遍历的集合
  • predicate (function | string | number | Array | Object, optional) — 判断函数或简写

Returns

  • * — 第一个匹配元素;没有匹配时返回 undefined

Throws

  • TypeError — 集合或 predicate 类型不正确时抛出

flatMap( collection: Array | Object, iteratee: function | string | number | Array | Object, ): Array

映射集合并将每个结果展开一层。

Parameters

  • collection (Array | Object) — 要遍历的集合
  • iteratee (function | string | number | Array | Object) — 转换函数或简写

Returns

  • Array — 展开后的结果数组

Throws

  • TypeError — 参数类型不正确时抛出

groupBy( collection: Array | Object, iteratee?: function | string | number | Array | Object, ): Object

按 iteratee 结果将集合元素分组。

Parameters

  • collection (Array | Object) — 要遍历的集合
  • iteratee (function | string | number | Array | Object, optional) — 分组函数或简写

Returns

  • Object — 分组结果对象

Throws

  • TypeError — 集合或 iteratee 类型不正确时抛出

includes( collection: Array | Object | string, value: *, fromIndex?: number, ): boolean

判断集合是否包含指定值,比较遵循 SameValueZero 规则。

Parameters

  • collection (Array | Object | string) — 要检查的集合或字符串
  • value (*) — 要查找的值
  • fromIndex (number, optional, default: 0) — 数组或字符串的起始索引

Returns

  • boolean — 是否包含该值

Throws

  • TypeError — 集合类型不支持时抛出

keyBy( collection: Array | Object, iteratee?: function | string | number | Array | Object, ): Object

按 iteratee 结果建立元素索引,重复键保留最后一个元素。

Parameters

  • collection (Array | Object) — 要遍历的集合
  • iteratee (function | string | number | Array | Object, optional) — 索引函数或简写

Returns

  • Object — 索引结果对象

Throws

  • TypeError — 集合或 iteratee 类型不正确时抛出

keys(object: Object): Array.<string>

static
keys.js:9

返回对象的可枚举自有属性名。

Parameters

  • object (Object) — 要读取的普通对象

Returns

  • Array.<string> — 属性名数组

Throws

  • TypeError — 参数不是对象时抛出

map( collection: Array | Object, iteratee?: function | string | number | Array | Object, ): Array

static
map.js:12

将转换函数应用于集合的每个元素。

Parameters

  • collection (Array | Object) — 要遍历的集合
  • iteratee (function | string | number | Array | Object, optional) — 转换函数或简写

Returns

  • Array — 转换结果数组

Throws

  • TypeError — 集合或 iteratee 类型不正确时抛出

partition( collection: Array | Object, predicate?: function | string | number | Array | Object, ): Array.<Array.<*>>

将集合拆分为满足条件和不满足条件的两个数组。

Parameters

  • collection (Array | Object) — 要遍历的集合
  • predicate (function | string | number | Array | Object, optional) — 判断函数或简写

Returns

  • Array.<Array.<*>> — [满足条件元素, 其余元素]

Throws

  • TypeError — 集合或 predicate 类型不正确时抛出

range( start: number, end?: number, step?: number, ): Array.<number>

生成一个不包含终点的等差数列。

Parameters

  • start (number) — 起点;仅传一个参数时该参数作为终点且起点为 0
  • end (number, optional) — 终点
  • step (number, optional) — 步长,省略时根据方向自动取 1 或 -1

Returns

  • Array.<number> — 数列数组

Throws

  • TypeError | RangeError — 参数不是有限数值或步长为零时抛出

reduce( collection: Array | Object, reducer: function, initialValue?: *, ): *

依次将集合元素归并为一个结果值。

Parameters

  • collection (Array | Object) — 要遍历的数组或普通对象
  • reducer (function) — 归并函数,参数为累计值、当前值、键和原集合
  • initialValue (*, optional) — 初始累计值;省略时使用第一个元素

Returns

  • * — 归并结果;空集合且省略初始值时返回 undefined

Throws

  • TypeError — 集合或归并函数类型不正确时抛出

reject( collection: Array | Object, predicate?: function | string | number | Array | Object, ): Array

返回不满足条件的集合元素。

Parameters

  • collection (Array | Object) — 要遍历的数组或普通对象
  • predicate (function | string | number | Array | Object, optional) — 判断函数或 iteratee 简写

Returns

  • Array — 被拒绝条件的元素数组

Throws

  • TypeError — 集合或 predicate 类型不正确时抛出

size( collection: Array | Object | string | Map | Set | null | undefined, ): number

static
size.js:9

返回集合的元素数量。

Parameters

  • collection (Array | Object | string | Map | Set | null | undefined) — 要统计的集合

Returns

  • number — 元素数量;null 和 undefined 返回 0

Throws

  • TypeError — 不支持的集合类型时抛出

some( collection: Array | Object, predicate?: function | string | number | Array | Object, ): boolean

判断集合中是否存在至少一个满足条件的元素。

Parameters

  • collection (Array | Object) — 要遍历的集合
  • predicate (function | string | number | Array | Object, optional) — 判断函数或简写

Returns

  • boolean — 存在匹配元素时为 true

Throws

  • TypeError — 集合或 predicate 类型不正确时抛出

sortBy( collection: Array | Object, ...iteratees: function | string | number | Array | Object, ): Array

根据一个或多个 iteratee 对集合进行稳定升序排序。

Parameters

  • collection (Array | Object) — 要排序的数组或普通对象
  • iteratees (function | string | number | Array | Object) — 排序依据

Returns

  • Array — 排序后的新数组,不修改输入集合

Throws

  • TypeError — 集合或 iteratee 类型不正确时抛出

values(object: Object): Array

返回对象的可枚举自有属性值。

Parameters

  • object (Object) — 要读取的普通对象

Returns

  • Array — 属性值数组

Throws

  • TypeError — 参数不是对象时抛出