Appearance
super-tools-lib - v1.74.0 / Modules / array
Module: array
Table of contents
Functions
- arrToObject
- arrayUnique
- bubbleSort
- contains
- countBy
- deduplicateArray
- depthTraversal
- fastFilter
- flattenArray
- indexOfMax
- indexOfMin
- last
- reduce
- shuffle
- sortArray
- toNumbers
Functions
arrToObject
▸ arrToObject<T
, K
>(arr
, key
): Record
<string
, T
>
将数组转换为对象
Since
1.0.0
Example
ts
import { arrToObject } from 'super-tools-lib'
const arr = [{a:2},{a:3}]
arrToObject(arr, 'a') // {2: {a:2}, 3: {a:3}}
Type parameters
Name | Type |
---|---|
T | extends Record <string , any > |
K | extends string | number | symbol |
Parameters
Name | Type | Description |
---|---|---|
arr | T [] | 数组 |
key | K | 对象的key |
Returns
Record
<string
, T
>
返回数组转换后的对象
arrayUnique
▸ arrayUnique<T
>(arr
): T
[]
数组去重
- 支持引用数据类型的深度对比
Since
1.0.0
Example
ts
import { arrayUnique } from 'super-tools-lib'
const arr = [1,2,2,2,3,1]
arrayUnique(arr) // [1, 2, 3]
const arr2 = [{name: 'Olivia'}, {name: 'Sophia'}, {name: 'Olivia'}]
arrayUnique(arr2) // [{name: 'Olivia'}, {name: 'Sophia'}]
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
arr | T [] | 数组 |
Returns
T
[]
返回去重后的数组
bubbleSort
▸ bubbleSort<T
>(arr
, iteratee
): T
[]
冒泡排序
Since
1.0.0
Example
ts
import { bubbleSort } from 'super-tools-lib'
const arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
const sortedArr = bubbleSort(arr, (v1, v2)=> v1 > v2)
console.log(sortedArr) // [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
const arr2 = [{age:10},{age:8},{age: 18}]
const sortedArr2 = bubbleSort(arr,(v1, v2)=>v1.age > v2.age)
console.log(sortedArr2) // [{age:8},{age:10},{age: 18}]
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
arr | T [] | 待排序数组 |
iteratee | (v1 : T , v2 : T ) => boolean | 每次迭代调用的函数 (v1第一个值, v2第二个值)=> 返回true则需要调换位置 |
Returns
T
[]
返回排序后的数组
contains
▸ contains<T
>(arr
, criteria
): boolean
检查数组是否包含符合某些标准的值
Since
1.0.0
Example
ts
import { contains } from 'super-tools-lib'
const arr = [1,2,3,4]
contains(arr, (item) => item > 3) // true
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
arr | T [] | 数组 |
criteria | (a : T ) => boolean | 标准 |
Returns
boolean
返回是否包含
countBy
▸ countBy<T
, K
>(arr
, key
): Record
<string
, number
>
按数组对象中的属性计数
Since
1.0.0
Example
ts
import { countBy } from 'super-tools-lib'
const arr = [{age: 12},{age: 12}, {age: 10}]
countBy(arr) // {10: 1, 12: 2}
Type parameters
Name | Type |
---|---|
T | extends Record <string , string > |
K | extends string | number | symbol |
Parameters
Name | Type | Description |
---|---|---|
arr | T [] | 数组 |
key | K | - |
Returns
Record
<string
, number
>
- 返回统计数量
deduplicateArray
▸ deduplicateArray<T
>(arr
, key?
): T
[]
数组去重
Since
1.74.0
Example
ts
import { deduplicateArray } from 'super-tools-lib'
const arr = [1,2,2,2,3,1]
deduplicateArray(arr) // [1, 2, 3]
const arr2 = [{name: 'Olivia'}, {name: 'Sophia'}, {name: 'Olivia'}]
deduplicateArray(arr2, 'name') // [{name: 'Olivia'}, {name: 'Sophia'}]
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
arr | T [] | 数组 |
key? | string | 去重的key |
Returns
T
[]
返回去重后的数组
depthTraversal
▸ depthTraversal(data
, callback
, childrenKey?
): void
深度优先遍历
Since
1.63.0
Example
ts
import { depthTraversal } from 'super-tools-lib'
const data = [
{
name: '1',
children: [
{
name: '1-1',
children: [
{
name: '1-1-1',
children: [
{ name: '1-1-1-1' },
],
},
],
},
],
},
{
name: '2',
},
]
depthTraversal(data, (item, index, arr) => {
console.log(item.name)
})
// 1
// 1-1
// 1-1-1
// 1-1-1-1
// 2
Parameters
Name | Type | Default value | Description |
---|---|---|---|
data | any [] | undefined | 需要遍历的数组 |
callback | (item : any , index : number , arr : any []) => void | undefined | 回调函数 |
childrenKey | string | 'children' | 子节点的key |
Returns
void
fastFilter
▸ fastFilter<T
>(data
, iteratee
): any
[]
快速过滤,过滤数组中符合条件的元素
- 不同于 Array.prototype.filter,该方法会同步遍历数组的前半部分和后半部分
Since
1.67.0
Example
ts
import { fastFilter } from 'super-tools-lib'
fastFilter([1,2,3], (i) => i <= 2 ) // [1,2]
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
data | T [] |
iteratee | (item : T , key? : string | number ) => boolean |
Returns
any
[]
flattenArray
▸ flattenArray<T
>(arr
): T
[]
数组扁平化
Since
1.61.0
Example
ts
import { flattenArray } from 'super-tools-lib'
const arr = [[1],2,[3,[4]]]
flattenArray(arr) // [1,2,3,4]
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
arr | T [] | 数组 |
Returns
T
[]
T[] 返回一维数组
indexOfMax
▸ indexOfMax(arr
): number
查找数组中最大项的索引
Since
1.0.0
Example
ts
import { indexOfMax } from 'super-tools-lib'
const arr = [1,2,199,22]
indexOfMax(arr) // 2
Parameters
Name | Type | Description |
---|---|---|
arr | number [] | 数组 |
Returns
number
返回数组中最大项的索引
indexOfMin
▸ indexOfMin(arr
): number
查找数组中最小项的索引
Since
1.0.0
Example
ts
import { indexOfMin } from 'super-tools-lib'
const arr = [1,2,199,22]
indexOfMin(arr) // 0
Parameters
Name | Type | Description |
---|---|---|
arr | number [] | 数组 |
Returns
number
返回数组中最小项的索引
last
▸ last<T
>(array
): T
返回数组中最后一项
Since
1.0.0
Example
ts
import { last } from 'super-tools-lib'
const arr = [1,2,3]
last(arr) // 3
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
array | T [] | 数组 |
Returns
T
返回数组的最后一项
reduce
▸ reduce<T
>(data
, iteratee
, init?
): number
reduce
Since
1.62.0
Example
ts
import { reduce } from 'super-tools-lib'
const arr = [1,2,3,4,5]
const num = reduce(arr, (pre,cur)=> pre+cur)
// num => 15
const num = reduce(arr, (pre,cur)=> pre+cur, 100)
// num => 115
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
data | T [] | 数组 |
iteratee | (pre : number | T , cur : T , index : number , arr : T []) => number | 每次迭代的回调函数 |
init? | number | 初始值 |
Returns
number
返回计算结果
shuffle
▸ shuffle<T
>(arr
): T
[]
随机打乱数组顺序
- 洗牌算法随机
Since
1.62.0
Example
ts
import { strInversion } from 'super-tools-lib'
strInversion('abc') // cba
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
arr | T [] | 要操作的数组 |
Returns
T
[]
返回打乱顺序后的数组
sortArray
▸ sortArray<T
>(data
, keyword
, order?
): T
[]
数组排序
- 支持按字母顺序排序
Since
1.0.0
Example
ts
import { sortArray } from 'super-tools-lib'
const arr = [{name: 'c'}, {name: 'b'}, {name: 'a'}, {name: 'd'}]
sortArray(arr, 'name') // [{name: 'a'}, {name: 'b'}, {name: 'c'}, {name: 'd'}]
sortArray(arr, 'name', 'desc') // [{name: 'a'}, {name: 'b'}, {name: 'c'}, {name: 'd'}]
Type parameters
Name |
---|
T |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
data | T [] | undefined | 待排序数组 |
keyword | string | undefined | 排序关键字 |
order? | string | 'asc' | 排序方式,asc为升序,desc为降序 |
Returns
T
[]
返回排序后的数组
toNumbers
▸ toNumbers(arr
): number
[]
将字符串数组转换为数字数组
Since
1.0.0
Example
ts
import { arrToObject } from 'super-tools-lib'
const arr = ['1', '2', '3']
arrToObject(arr) // [1, 2, 3]
Parameters
Name | Type | Description |
---|---|---|
arr | string [] | 字符串数组 |
Returns
number
[]
返回转换后的数字数组