Module: utils/src/modules/__indexArrays
Functions
chunk
▸ chunk<V
>(array
, size?
): TypeOfList
<V
>[][]
返回一个数组,数组由给定数组拆分成多个size
长度的区块组成。 如果给定数组无法被分割成全部等长的区块,那么最后剩余的元素将组成一个区块。
Example
chunk([1, 2, 3, 4, 5], 2)
=> [[1, 2], [3, 4], [5]]
chunk([1, 2, 3, 4, 5], 2)
=> [[1, 2], [3, 4], [5]]
Type parameters
Name | Type |
---|---|
V | extends List <any , V > |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
array | V | undefined | 给定数组 |
size | number | 1 | 区块大小 |
Returns
TypeOfList
<V
>[][]
compact
▸ compact<V
>(array
): Truthy
<TypeOfList
<V
>>
返回一个数组,数组由给定数组中所有的非假值元素组成。例如false
,null
,0
,""
,undefined
和NaN
都被认为是“假值”。
Example
compact([0, 1, false, 2, '', 3])
=> [1, 2, 3]
compact([0, 1, false, 2, '', 3])
=> [1, 2, 3]
Type parameters
Name | Type |
---|---|
V | extends List <any , V > |
Parameters
Name | Type | Description |
---|---|---|
array | V | 给定数组 |
Returns
Truthy
<TypeOfList
<V
>>
difference
▸ difference<V
>(array
, ...values
): TypeOfCollection
<V
, never
>[]
返回一个数组,数组由给定数组与values
进行差集运算后组成(使用===
表达式做相等测试)。
Example
difference([1, 2, 1, 0, 3, 1, 4], 0, 1)
=> [2, 3, 4]
difference([1, 2, 3, 4, 5], [2, 3], [4])
=> [1, 5]
difference([1, 2, 1, 0, 3, 1, 4], 0, 1)
=> [2, 3, 4]
difference([1, 2, 3, 4, 5], [2, 3], [4])
=> [1, 5]
Type parameters
Name | Type |
---|---|
V | extends List <any , V > |
Parameters
Name | Type | Description |
---|---|---|
array | V | 给定数组 |
...values | unknown [] | 任意值的集合 |
Returns
TypeOfCollection
<V
, never
>[]
findIndex
▸ findIndex<V
>(array
, predicate?
, context?
): number
返回给定数组中第一个通过predicate
真值检测的索引,如果没有元素通过检测则返回-1
Example
findIndex([4, 6, 7, 12], (item) => item === 7)
=> 1
findIndex([4, 6, 8, 12], (item) => item === 1)
=> -1
findIndex([4, 6, 7, 12], (item) => item === 7)
=> 1
findIndex([4, 6, 8, 12], (item) => item === 1)
=> -1
Type parameters
Name | Type |
---|---|
V | extends List <any , V > |
Parameters
Name | Type | Description |
---|---|---|
array | V | 给定数组 |
predicate? | Iteratee <V , boolean > | 谓语迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为谓语迭代器函数的执行上下文 this |
Returns
number
findLastIndex
▸ findLastIndex<V
>(array
, predicate?
, context?
): number
反向迭代数组,返回给定数组中第一个通过predicate
真值检测的索引,如果没有元素通过检测则返回-1
Example
findLastIndex([4, 6, 7, 12], (item) => item === 7)
=> 2
findLastIndex([4, 6, 8, 12], (item) => item === 1)
=> -1
findLastIndex([4, 6, 7, 12], (item) => item === 7)
=> 2
findLastIndex([4, 6, 8, 12], (item) => item === 1)
=> -1
Type parameters
Name | Type |
---|---|
V | extends List <any , V > |
Parameters
Name | Type | Description |
---|---|---|
array | V | 需操作数组 |
predicate? | Iteratee <V , boolean > | 谓语迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为谓语迭代器函数的执行上下文 this |
Returns
number
flatten
▸ flatten<V
>(array
, depth?
): DeepestListItemOrSelf
<TypeOfList
<V
>>[]
推荐原生:Array.prototype.flat。 返回一个数组,数组由给定数组扁平化后组成,传递depth
控制递归深度。
Example
flatten([1, [2], [3, [[4]]]])
=> [1, 2, 3, 4]
flatten([1, [2], [3, [[4]]]], 1)
=> [1, 2, 3, [[4]]]
flatten([1, [2], [3, [[4]]]])
=> [1, 2, 3, 4]
flatten([1, [2], [3, [[4]]]], 1)
=> [1, 2, 3, [[4]]]
Type parameters
Name | Type |
---|---|
V | extends List <any , V > |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
array | V | undefined | 给定数组 |
depth | number | Infinity | 递归深度,默认 Infinity |
Returns
DeepestListItemOrSelf
<TypeOfList
<V
>>[]
head
▸ head<V
>(array
): undefined
| TypeOfList
<V
>
返回给定数组的第一个元素,无数据返回undefined
。
Example
head([1, 2, 3])
=> 1
head([1, 2, 3])
=> 1
Type parameters
Name | Type |
---|---|
V | extends List <any , V > |
Parameters
Name | Type | Description |
---|---|---|
array | V | 给定数组 |
Returns
undefined
| TypeOfList
<V
>
indexOf
▸ indexOf<V
>(array
, value?
, isSortedOrFromIndex?
): number
推荐原生:Array.prototype.indexOf。 返回value
在给定数组中的索引,如果value
不存在则返回-1
,若数组已升序则推荐isSortedOrFromIndex
参数传递true
使用二分查找。
Example
indexOf([1, 2, 3], 2)
=> 1
indexOf([1, 2, 3], 4)
=> -1
indexOf([1, 2, 3], 2)
=> 1
indexOf([1, 2, 3], 4)
=> -1
Type parameters
Name | Type |
---|---|
V | extends List <any , V > |
Parameters
Name | Type | Description |
---|---|---|
array | V | 给定数组 |
value? | TypeOfList <V > | 需检索值 |
isSortedOrFromIndex? | number | boolean | 起始索引,或传递 true 进行二分查找(数组已升序) |
Returns
number
initial
▸ initial<V
>(array
): never
[] | TypeOfList
<V
>
返回一个数组,数组由给定数组中除了最后一个元素之外的所有元素(去除给定数组中的最后一个元素)组成。
Example
initial([1, 2, 3])
=> [1, 2]
initial([1, 2, 3])
=> [1, 2]
Type parameters
Name | Type |
---|---|
V | extends List <any , V > |
Parameters
Name | Type | Description |
---|---|---|
array | V | 给定数组 |
Returns
never
[] | TypeOfList
<V
>
intersection
▸ intersection<V
>(...arrays
): V
[]
返回一个数组,数组由给定数组集合的交集组成。
Example
intersection([1, 2, 3], [101, 2, 1, 10], [2, 1])
=> [1, 2]
intersection([1, 2, 3])
=> [1, 2, 3]
intersection([1, 2, 3], [101, 2, 1, 10], [2, 1])
=> [1, 2]
intersection([1, 2, 3])
=> [1, 2, 3]
Type parameters
Name | Type |
---|---|
V | extends List <any , V > |
Parameters
Name | Type | Description |
---|---|---|
...arrays | V [] | 给定数组集合 |
Returns
V
[]
last
▸ last<V
>(array
): undefined
| TypeOfList
<V
>[]
返回给定数组的最后一个元素,无数据返回undefined
。
Example
last([1, 2, 3])
=> 3
last([1, 2, 3])
=> 3
Type parameters
Name | Type |
---|---|
V | extends List <any , V > |
Parameters
Name | Type | Description |
---|---|---|
array | V | 给定数组 |
Returns
undefined
| TypeOfList
<V
>[]
lastIndexOf
▸ lastIndexOf<V
>(array
, value?
, isSortedOrFromIndex?
): number
推荐原生:Array.prototype.lastIndexOf。 反向迭代数组,返回value
在给定数组中的索引,如果value
不存在则返回-1
,若数组已升序则推荐isSortedOrFromIndex
参数传递true
使用二分查找。
Example
lastIndexOf([1, 2, 3, 1, 2, 3], 2)
=> 4
lastIndexOf([1, 2, 3, 1, 2, 3], 5)
=> -1
lastIndexOf([1, 2, 3, 1, 2, 3], 2)
=> 4
lastIndexOf([1, 2, 3, 1, 2, 3], 5)
=> -1
Type parameters
Name | Type |
---|---|
V | extends List <any , V > |
Parameters
Name | Type | Description |
---|---|---|
array | V | 给定数组 |
value? | TypeOfList <V > | 需检索值 |
isSortedOrFromIndex? | number | boolean | 起始索引,或传递 true 进行二分查找(数组已升序) |
Returns
number
object
▸ object<V
>(array
): Dictionary
<PairValue
<TypeOfList
<V
>>>
推荐原生:Object.fromEntries。 返回一个对象,对象由给定键-值对数组转换后组成。entries
的反函数。如果存在重复键,最后一个值将被返回。
Example
object([['moe', 30], ['larry', 40], ['curly', 50]])
=> {moe: 30, larry: 40, curly: 50}
object([['moe', 30], ['larry', 40], ['curly', 50]])
=> {moe: 30, larry: 40, curly: 50}
Type parameters
Name | Type |
---|---|
V | extends List <List <any >, V > |
Parameters
Name | Type | Description |
---|---|---|
array | V | 给定键-值对数组 |
Returns
Dictionary
<PairValue
<TypeOfList
<V
>>>
range
▸ range(start
, stop?
, step?
): number
[]
返回一个数组,数组由start
到stop
之间的整数组成,如果没有传递stop
,则start
作为结束值,0
为起始。 用step
来增加(或减少)步长,便于forEach
和map
循环。 如果需要一个负数的值域 ,请使用负数step
。
Example
range(10)
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(1, 11)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
range(0, 30, 5)
=> [0, 5, 10, 15, 20, 25]
range(0, -10, -1)
=> [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
range(0)
=> []
range(10)
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(1, 11)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
range(0, 30, 5)
=> [0, 5, 10, 15, 20, 25]
range(0, -10, -1)
=> [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
range(0)
=> []
Parameters
Name | Type | Description |
---|---|---|
start | number | 起始值 |
stop? | number | 结束值 |
step? | number | 步长 |
Returns
number
[]
sortedIndex
▸ sortedIndex<V
>(array
, value
, iteratee?
, context?
): number
使用二分查找返回value
在给定数组(数组已升序)对应处的索引,常用在需要确定value
在给定数组中的位置序号,value
按此序号插入能保持给定数组原有的排序。
Example
sortedIndex([10, 20, 30, 40, 50], 35)
=> 3
const stooges = [{name: 'moe', age: 40}, {name: 'curly', age: 60}]
sortedIndex(stooges, {name: 'larry', age: 50}, 'age')
=> 1
sortedIndex([10, 20, 30, 40, 50], 35)
=> 3
const stooges = [{name: 'moe', age: 40}, {name: 'curly', age: 60}]
sortedIndex(stooges, {name: 'larry', age: 50}, 'age')
=> 1
Type parameters
Name | Type |
---|---|
V | extends List <any , V > |
Parameters
Name | Type | Description |
---|---|---|
array | V | 给定数组 |
value | TypeOfList <V > | 需检索值 |
iteratee? | Iteratee <V , unknown > | 迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为迭代器函数的执行上下文 this |
Returns
number
tail
▸ tail<V
>(array
): TypeOfList
<V
>[]
返回一个数组,数组由给定数组中除了第一个元素外之外的所有元素(去除给定数组中的第一个元素)组成。
Example
tail([1, 2, 3])
=> [2, 3]
tail([1, 2, 3])
=> [2, 3]
Type parameters
Name | Type |
---|---|
V | extends List <any , V > |
Parameters
Name | Type | Description |
---|---|---|
array | V | 给定数组 |
Returns
TypeOfList
<V
>[]
union
▸ union<V
>(...arrays
): List
<V
>[]
返回一个数组,数组由给定数组集合的并集组成,uniq
的简化版本且接收参数为数组的集合。 当处理数据量小(length < 100,000)时,推荐使用Array.from(new Set(array))
。
Example
union([1, 2, 3], [101, 2, 1, 10], [2, 1])
=> [1, 2, 3, 101, 10]
union([1, 2, 3], [101, 2, 1, 10], [2, 1])
=> [1, 2, 3, 101, 10]
Type parameters
Name |
---|
V |
Parameters
Name | Type | Description |
---|---|---|
...arrays | List <V >[] | 给定数组集合 |
Returns
List
<V
>[]
uniq
▸ uniq<V
, I
>(array
, isSorted?
, iteratee?
, context?
): TypeOfList
<V
>[]
返回一个数组,数组由给定数组的并集组成,使用===
做相等测试。 如果要处理对象元素, 需要传递iteratee
函数来获取要对比的属性。
Example
uniq([1, 2, 1, 4, 1, 3])
=> [1, 2, 4, 3]
uniq([1, 2, 1, 4, 1, 3])
=> [1, 2, 4, 3]
Type parameters
Name | Type |
---|---|
V | extends List <any , V > |
I | extends Iteratee <V , unknown > |
Parameters
Name | Type | Description |
---|---|---|
array | V | 给定数组 |
isSorted? | boolean | 是否启用更快的算法(数组已升序) |
iteratee? | I | 迭代器函数,通过 iteratee 进行转换,以简化速记语法 |
context? | unknown | 上下文对象,若传递,则作为迭代器函数的执行上下文 this |
Returns
TypeOfList
<V
>[]
unzip
▸ unzip<V
>(arrays
): PropertyTypeOrAny
<TypeOfCollection
<V
, never
>, number
>[][]
返回一个数组,数组由给定数组按索引分组组成。zip
的反函数。
Example
unzip([['moe', 30, true], ['larry', 40, false], ['curly', 50, false]])
=> [['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]]
unzip([['moe', 30, true], ['larry', 40, false], ['curly', 50, false]])
=> [['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]]
Type parameters
Name | Type |
---|---|
V | extends List <List <any >, V > |
Parameters
Name | Type | Description |
---|---|---|
arrays | V | 给定数组 |
Returns
PropertyTypeOrAny
<TypeOfCollection
<V
, never
>, number
>[][]
zip
▸ zip<V
>(...arrays
): PropertyTypeOrAny
<V
, number
>[][]
返回一个数组,数组由给定数组集合按索引分组汇总组成。
Example
zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false])
=> [['moe', 30, true], ['larry', 40, false], ['curly', 50, false]]
zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false])
=> [['moe', 30, true], ['larry', 40, false], ['curly', 50, false]]
Type parameters
Name | Type |
---|---|
V | extends List <any , V > |
Parameters
Name | Type | Description |
---|---|---|
...arrays | V [] | 给定数组集合 |
Returns
PropertyTypeOrAny
<V
, number
>[][]