Functions
toDictionary()
支持对象和数组转为字典
Param
对象或数组
Param
配置项
Example
// 对象转字典
const object = { a: { value: 1, text: 'A' }, b: { value: 2, text: 'B' } }
// 对象转字典时以对象键作为字典键
const dict = toDictionary(object, { valueKey: 'value', labelKey: 'text' })
dict.raw === object
// => true
dict.get('a')
// => { key: 'a', label: 'A', value: 1, data: { value: 1, text: 'A' } }
// 数组转字典
const array = [{ id: 1, name: 'a' }, { id: 2, name: 'b' }]
// 设置 name 为字典键
const dict = toDictionary(array, { valueKey: 'id', labelKey: 'name', key: 'name' })
dict.raw === array
// => true
dict.get('a')
// => { key: 'a', label: 'a', value: 1, data: { id: 1, name: 'a' } }
Call Signature
toDictionary<
Data
,ValueKey
,LabelKey
,Key
,Value
,DataKey
,Methods
>(array
,options
?):Dictionary
<Data
,Value
,DataKey
,Methods
,Data
[]>
数组转字典
Type Parameters
Type Parameter | Default type |
---|---|
Data | - |
ValueKey extends string | "value" |
LabelKey extends string | ValueKey |
Key extends string | ValueKey |
Value | Data extends Primitive ? Data <Data > : Data extends Record <ValueKey , Value > ? Value : unknown |
DataKey extends string | Data extends Primitive ? Data <Data > extends string ? string & Data <Data > : string : string |
Methods extends Record <PropertyKey , Fn > | object |
Parameters
Parameter | Type | Description |
---|---|---|
array | Data [] | 数组 |
options ? | DictionaryOptions <ValueKey , LabelKey , Key , Methods , Dictionary <Data , Value , DataKey , Methods , Data []>> | 配置项 |
Returns
Dictionary
<Data
, Value
, DataKey
, Methods
, Data
[]>
字典
Param
对象或数组
Param
配置项
Examples
// 对象转字典
const object = { a: { value: 1, text: 'A' }, b: { value: 2, text: 'B' } }
// 对象转字典时以对象键作为字典键
const dict = toDictionary(object, { valueKey: 'value', labelKey: 'text' })
dict.raw === object
// => true
dict.get('a')
// => { key: 'a', label: 'A', value: 1, data: { value: 1, text: 'A' } }
// 数组转字典
const array = [{ id: 1, name: 'a' }, { id: 2, name: 'b' }]
// 设置 name 为字典键
const dict = toDictionary(array, { valueKey: 'id', labelKey: 'name', key: 'name' })
dict.raw === array
// => true
dict.get('a')
// => { key: 'a', label: 'a', value: 1, data: { id: 1, name: 'a' } }
const array = [{ id: 1, name: 'a' }, { id: 2, name: 'b' }]
// 设置 name 为字典键
const dict = toDictionary(array, { valueKey: 'id', labelKey: 'name', key: 'name' })
dict.raw === array
// => true
dict.get('a')
// => { key: 'a', label: 'a', value: 1, data: { id: 1, name: 'a' } }
Call Signature
toDictionary<
Raw
,Data
,ValueKey
,LabelKey
,Key
,Value
,DataKey
,Methods
>(object
,options
?):Dictionary
<Data
,Value
,DataKey
,Methods
,Raw
>
对象转字典
Type Parameters
Type Parameter | Default type |
---|---|
Raw extends object | - |
Data | Raw extends Record <PropertyKey , Value > ? Value : unknown |
ValueKey extends string | "value" |
LabelKey extends string | ValueKey |
Key extends string | ValueKey |
Value | Data extends Primitive ? Data <Data > : Data extends Record <ValueKey , Value > ? Value : unknown |
DataKey extends string | string & Record <never , never > | KeyOf <Raw , string > |
Methods extends Record <PropertyKey , Fn > | object |
Parameters
Parameter | Type | Description |
---|---|---|
object | Raw | 对象 |
options ? | Omit <DictionaryOptions <ValueKey , LabelKey , Key , Methods , Dictionary <Data , Value , DataKey , Methods , Raw >>, "key" > | 配置项 |
Returns
Dictionary
<Data
, Value
, DataKey
, Methods
, Raw
>
字典
Param
对象或数组
Param
配置项
Examples
// 对象转字典
const object = { a: { value: 1, text: 'A' }, b: { value: 2, text: 'B' } }
// 对象转字典时以对象键作为字典键
const dict = toDictionary(object, { valueKey: 'value', labelKey: 'text' })
dict.raw === object
// => true
dict.get('a')
// => { key: 'a', label: 'A', value: 1, data: { value: 1, text: 'A' } }
// 数组转字典
const array = [{ id: 1, name: 'a' }, { id: 2, name: 'b' }]
// 设置 name 为字典键
const dict = toDictionary(array, { valueKey: 'id', labelKey: 'name', key: 'name' })
dict.raw === array
// => true
dict.get('a')
// => { key: 'a', label: 'a', value: 1, data: { id: 1, name: 'a' } }
const object = { a: { value: 1, text: 'A' }, b: { value: 2, text: 'B' } }
// 对象转字典时以对象键作为字典键
const dict = toDictionary(object, { valueKey: 'value', labelKey: 'text' })
dict.raw === object
// => true
dict.get('a')
// => { key: 'a', label: 'A', value: 1, data: { value: 1, text: 'A' } }
Interfaces
DictionaryBase<Raw, Data, Value>
基础字典
Type Parameters
Type Parameter | Default type |
---|---|
Raw | unknown |
Data | unknown |
Value | unknown |
Properties
Property | Modifier | Type | Description |
---|---|---|---|
map | readonly | Map <string , DictionaryItem <Data , Value >> | 字典内置原始数据映射对象 |
raw | readonly | Raw | 字典原始数据 |
size | readonly | number | 字典项数量 |
DictionaryBuiltinMethods<Data, Value, Key, Item>
字典内置方法
Extends
Pick
<Item
[],"forEach"
|"filter"
|"find"
|"every"
|"some"
>
Type Parameters
Type Parameter | Default type |
---|---|
Data | unknown |
Value | unknown |
Key extends string | string |
Item extends DictionaryItem | DictionaryItem <Data , Value > |
Methods
every()
Call Signature
every<
S
>(predicate
,thisArg
?):this is S[]
Determines whether all the members of an array satisfy the specified test.
Type Parameters
Type Parameter |
---|
S extends DictionaryItem <unknown , unknown > |
Parameters
Parameter | Type | Description |
---|---|---|
predicate | (value , index , array ) => value is S | A function that accepts up to three arguments. The every method calls the predicate function for each element in the array until the predicate returns a value which is coercible to the Boolean value false, or until the end of the array. |
thisArg ? | any | An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value. |
Returns
this is S[]
Inherited from
Pick.every
Call Signature
every(
predicate
,thisArg
?):boolean
Determines whether all the members of an array satisfy the specified test.
Parameters
Parameter | Type | Description |
---|---|---|
predicate | (value , index , array ) => unknown | A function that accepts up to three arguments. The every method calls the predicate function for each element in the array until the predicate returns a value which is coercible to the Boolean value false, or until the end of the array. |
thisArg ? | any | An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value. |
Returns
boolean
Inherited from
Pick.every
filter()
Call Signature
filter<
S
>(predicate
,thisArg
?):S
[]
Returns the elements of an array that meet the condition specified in a callback function.
Type Parameters
Type Parameter |
---|
S extends DictionaryItem <unknown , unknown > |
Parameters
Parameter | Type | Description |
---|---|---|
predicate | (value , index , array ) => value is S | A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array. |
thisArg ? | any | An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value. |
Returns
S
[]
Inherited from
Pick.filter
Call Signature
filter(
predicate
,thisArg
?):Item
[]
Returns the elements of an array that meet the condition specified in a callback function.
Parameters
Parameter | Type | Description |
---|---|---|
predicate | (value , index , array ) => unknown | A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array. |
thisArg ? | any | An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value. |
Returns
Item
[]
Inherited from
Pick.filter
find()
Call Signature
find<
S
>(predicate
,thisArg
?):undefined
|S
Returns the value of the first element in the array where predicate is true, and undefined otherwise.
Type Parameters
Type Parameter |
---|
S extends DictionaryItem <unknown , unknown > |
Parameters
Parameter | Type | Description |
---|---|---|
predicate | (value , index , obj ) => value is S | find calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, find immediately returns that element value. Otherwise, find returns undefined. |
thisArg ? | any | If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead. |
Returns
undefined
| S
Inherited from
Pick.find
Call Signature
find(
predicate
,thisArg
?):undefined
|Item
Parameters
Parameter | Type |
---|---|
predicate | (value , index , obj ) => unknown |
thisArg ? | any |
Returns
undefined
| Item
Inherited from
Pick.find
forEach()
forEach(
callbackfn
,thisArg
?):void
Performs the specified action for each element in an array.
Parameters
Parameter | Type | Description |
---|---|---|
callbackfn | (value , index , array ) => void | A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array. |
thisArg ? | any | An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. |
Returns
void
Inherited from
Pick.forEach
some()
some(
predicate
,thisArg
?):boolean
Determines whether the specified callback function returns true for any element of an array.
Parameters
Parameter | Type | Description |
---|---|---|
predicate | (value , index , array ) => unknown | A function that accepts up to three arguments. The some method calls the predicate function for each element in the array until the predicate returns a value which is coercible to the Boolean value true, or until the end of the array. |
thisArg ? | any | An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value. |
Returns
boolean
Inherited from
Pick.some
Properties
DictionaryItem<Data, Value>
字典项
Type Parameters
Type Parameter | Default type |
---|---|
Data | unknown |
Value | unknown |
Properties
Property | Type | Description |
---|---|---|
data | Data | 字典项数据,来源于字典原始数据项 |
key | string | 字典项键 |
label | string | 字典项标签 |
value | Value | 字典项值 |
DictionaryOptions<ValueKey, LabelKey, Key, Methods, Instance>
字典配置项
Type Parameters
Type Parameter | Default type |
---|---|
ValueKey extends string | - |
LabelKey extends string | ValueKey |
Key extends string | ValueKey |
Methods extends Record <PropertyKey , Fn > | object |
Instance | Dictionary |
Properties
Type Aliases
Dictionary<Data, Value, Key, Methods, Raw>
Dictionary<
Data
,Value
,Key
,Methods
,Raw
> =DictionaryBase
<Raw
,Data
,Value
> &DictionaryBuiltinMethods
<Data
,Value
,Key
> &Methods
字典
Type Parameters
Type Parameter | Default type |
---|---|
Data | unknown |
Value | unknown |
Key extends string | string |
Methods extends Record <PropertyKey , Fn > | object |
Raw | Data [] | Record <string , Data > |