toDictionary
Namespaces
Functions
toDictionary()
toDictionary(array, options)
toDictionary<
Data
,ValueKey
,LabelKey
,Key
,Value
,DataKey
,Methods
>(array
,options
?):Dictionary
<Data
,Value
,DataKey
,Methods
,Data
[]>
数组转字典
Type parameters
Parameter | Value |
---|---|
Data | - |
ValueKey extends string | "value" |
LabelKey extends string | ValueKey |
Key extends string | ValueKey |
Value | Data extends Primitive ? Data : Data extends Record <ValueKey , Value > ? Value : unknown |
DataKey extends string | Data extends Primitive ? Data extends string ? string & Data : string : string |
Methods extends Record <PropertyKey , AnyFn > | 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
[]>
字典
Example
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' } }
toDictionary(object, options)
toDictionary<
Raw
,Data
,ValueKey
,LabelKey
,Key
,Value
,DataKey
,Methods
>(object
,options
?):Dictionary
<Data
,Value
,DataKey
,Methods
,Raw
>
对象转字典
Type parameters
Parameter | Value |
---|---|
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 extends Record <ValueKey , Value > ? Value : unknown |
DataKey extends string & Record <never , never > | KeyOf <Raw , string > |
Methods extends Record <PropertyKey , AnyFn > | 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
>
字典
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' } }
Interfaces
DictionaryBase<Raw, Data, Value>
基础字典
Type parameters
Parameter | Value |
---|---|
Raw | unknown |
Data | unknown |
Value | unknown |
Properties
Modifier | Property | Type | Description |
---|---|---|---|
readonly | map | Map <string , DictionaryItem <Data , Value >> | 字典内置原始数据映射对象 |
readonly | raw | Raw | 字典原始数据 |
readonly | size | number | 字典项数量 |
DictionaryBuiltinMethods<Data, Value, Key, Item>
字典内置方法
Extends
Pick
<Item
[],"forEach"
|"filter"
|"find"
|"every"
|"some"
>
Type parameters
Parameter | Value |
---|---|
Data | unknown |
Value | unknown |
Key extends string | string |
Item extends DictionaryItem | DictionaryItem <Data , Value > |
Methods
every()
every(predicate, thisArg)
every<
S
>(predicate
,thisArg
?):this is S[]
Determines whether all the members of an array satisfy the specified test.
Type parameters
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
every(predicate, thisArg)
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()
filter(predicate, thisArg)
filter<
S
>(predicate
,thisArg
?):S
[]
Returns the elements of an array that meet the condition specified in a callback function.
Type parameters
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
filter(predicate, thisArg)
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()
find(predicate, thisArg)
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
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
find(predicate, thisArg)
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
Property | Type | Description |
---|---|---|
entries | (orderParams ?) => [string , Item ][] | - |
eqValue | <T >(key , value ) => boolean | - |
get | <T >(key ) => undefined | Item | - |
getByValue | (value ) => undefined | Item | - |
getData | <T >(key ) => undefined | Item ["data" ] | - |
getDataByValue | (value ) => undefined | Item ["data" ] | - |
getKey | <T >(key ) => string | - |
getKeyByValue | (value ) => string | - |
getLabel | <T >(key ) => string | - |
getLabelByValue | (value ) => string | - |
getValue | <V , T >(key ) => undefined | V | - |
has | <T >(key ) => boolean | - |
items | (orderParams ?) => Item [] | - |
keys | (orderParams ?) => string [] | - |
labels | (orderParams ?) => string [] | - |
values | <T >(orderParams ?) => T [] | - |
DictionaryItem<Data, Value>
字典项
Type parameters
Parameter | Value |
---|---|
Data | unknown |
Value | unknown |
Properties
Property | Type | Description |
---|---|---|
data | Data | 字典项数据,来源于字典原始数据项 |
key | string | 字典项键 |
label | string | 字典项标签 |
value | Value | 字典项值 |
DictionaryOptions<ValueKey, LabelKey, Key, Methods, Instance>
字典配置项
Type parameters
Parameter | Value |
---|---|
ValueKey extends string | - |
LabelKey extends string | ValueKey |
Key extends string | ValueKey |
Methods extends Record <PropertyKey , AnyFn > | Object |
Instance | Dictionary |
Properties
Property | Type | Description |
---|---|---|
key? | Key | 字典键,值必须唯一 Default options.valueKey |
labelKey? | LabelKey | 字典项的标签键 Default options.valueKey |
methods? | Methods & ThisType <Instance > | 字典自定义方法 |
valueKey? | ValueKey | 字典项的值键,值必须唯一 Default 'value' |
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
Parameter | Value |
---|---|
Data | unknown |
Value | unknown |
Key extends string | string |
Methods extends Record <PropertyKey , AnyFn > | Object |
Raw | Data [] | Record <string , Data > |
DictionaryOrderByParams<T>
DictionaryOrderByParams<
T
>:_OrderByParams
<T
>
字典排序参数
Type parameters
Parameter | Value |
---|---|
T extends DictionaryItem <any , any > | DictionaryItem <any , any > |