Functions
arrayToMap()
数组转映射对象
Param
数组
Param
配置项
Example
ts
// 原始值数组转映射
const array = ['a', 'b', 'c']
arrayToMap(array)
// => Object { 'a': 'a', 'b': 'b', 'c': 'c' }
arrayToMap(array, { useMap: true })
// => Map [['a', 'a'], ['b', 'b'], ['c', 'c']]
// 对象数组转映射
const array = [{ id: 1, text: 'a' }, { id: 2, text: 'b' }]
arrayToMap(array, { primaryKey: 'id' })
// => Object { '1': { id: 1, text: 'a' }, '2': { id: 2, text: 'b' } }
arrayToMap(array, { primaryKey: 'id', useMap: true })
// => Map [[1, { id: 1, text: 'a' }], [2, { id: 2, text: 'b' }]]Call Signature
arrayToMap<
T,PrimaryKey,UseMap>(array,options):ArrayToMapResult<T[PrimaryKey],T,UseMap>
对象数组转映射对象
Type Parameters
| Type Parameter | Default type |
|---|---|
T extends object | - |
PrimaryKey extends string | number | symbol | keyof T |
UseMap extends boolean | false |
Parameters
| Parameter | Type | Description |
|---|---|---|
array | T[] | 对象数组 |
options | ArrayToMapOptions<UseMap, PrimaryKey extends string ? PrimaryKey<PrimaryKey> : never> | 配置项 |
Returns
ArrayToMapResult<T[PrimaryKey], T, UseMap>
Param
数组
Param
配置项
Examples
ts
// 原始值数组转映射
const array = ['a', 'b', 'c']
arrayToMap(array)
// => Object { 'a': 'a', 'b': 'b', 'c': 'c' }
arrayToMap(array, { useMap: true })
// => Map [['a', 'a'], ['b', 'b'], ['c', 'c']]
// 对象数组转映射
const array = [{ id: 1, text: 'a' }, { id: 2, text: 'b' }]
arrayToMap(array, { primaryKey: 'id' })
// => Object { '1': { id: 1, text: 'a' }, '2': { id: 2, text: 'b' } }
arrayToMap(array, { primaryKey: 'id', useMap: true })
// => Map [[1, { id: 1, text: 'a' }], [2, { id: 2, text: 'b' }]]ts
const array = [{ id: 1, text: 'a' }, { id: 2, text: 'b' }]
arrayToMap(array, { primaryKey: 'id' })
// => Object { '1': { id: 1, text: 'a' }, '2': { id: 2, text: 'b' } }
arrayToMap(array, { primaryKey: 'id', useMap: true })
// => Map [[1, { id: 1, text: 'a' }], [2, { id: 2, text: 'b' }]]Call Signature
arrayToMap<
T,UseMap>(array,options?):ArrayToMapResult<T,T,UseMap>
原始值数组转映射对象
Type Parameters
| Type Parameter | Default type |
|---|---|
T extends Primitive | - |
UseMap extends boolean | false |
Parameters
| Parameter | Type | Description |
|---|---|---|
array | T[] | 原始值数组 |
options? | Omit<ArrayToMapOptions<UseMap, string>, "primaryKey"> | 配置项 |
Returns
ArrayToMapResult<T, T, UseMap>
Param
数组
Param
配置项
Examples
ts
// 原始值数组转映射
const array = ['a', 'b', 'c']
arrayToMap(array)
// => Object { 'a': 'a', 'b': 'b', 'c': 'c' }
arrayToMap(array, { useMap: true })
// => Map [['a', 'a'], ['b', 'b'], ['c', 'c']]
// 对象数组转映射
const array = [{ id: 1, text: 'a' }, { id: 2, text: 'b' }]
arrayToMap(array, { primaryKey: 'id' })
// => Object { '1': { id: 1, text: 'a' }, '2': { id: 2, text: 'b' } }
arrayToMap(array, { primaryKey: 'id', useMap: true })
// => Map [[1, { id: 1, text: 'a' }], [2, { id: 2, text: 'b' }]]ts
const array = ['a', 'b', 'c']
arrayToMap(array)
// => Object { 'a': 'a', 'b': 'b', 'c': 'c' }
arrayToMap(array, { useMap: true })
// => Map [['a', 'a'], ['b', 'b'], ['c', 'c']]Interfaces
ArrayToMapOptions<UseMap, PrimaryKey>
Type Parameters
| Type Parameter | Default type |
|---|---|
UseMap extends boolean | false |
PrimaryKey extends string | string |
Properties
| Property | Type | Description |
|---|---|---|
primaryKey | PrimaryKey | 对象数组转映射的主键 |
useMap? | UseMap | 使用 Map 类型进行映射,设为 false 时使用 Object 类型映射 Default false |