Skip to content

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
ParameterValue
Data-
ValueKey extends string"value"
LabelKey extends stringValueKey
Key extends stringValueKey
ValueData extends Primitive ? Data : Data extends Record<ValueKey, Value> ? Value : unknown
DataKey extends stringData extends Primitive ? Data extends string ? string & Data : string : string
Methods extends Record<PropertyKey, AnyFn>Object
Parameters
ParameterTypeDescription
arrayData[]数组
options?DictionaryOptions<ValueKey, LabelKey, Key, Methods, Dictionary<Data, Value, DataKey, Methods, Data[]>>配置项
Returns

Dictionary<Data, Value, DataKey, Methods, Data[]>

字典

Example
ts
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
ParameterValue
Raw extends Object-
DataRaw extends Record<PropertyKey, Value> ? Value : unknown
ValueKey extends string"value"
LabelKey extends stringValueKey
Key extends stringValueKey
ValueData 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
ParameterTypeDescription
objectRaw对象
options?Omit<DictionaryOptions<ValueKey, LabelKey, Key, Methods, Dictionary<Data, Value, DataKey, Methods, Raw>>, "key">配置项
Returns

Dictionary<Data, Value, DataKey, Methods, Raw>

字典

Example
ts
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

ParameterValue
Rawunknown
Dataunknown
Valueunknown

Properties

ModifierPropertyTypeDescription
readonlymapMap<string, DictionaryItem<Data, Value>>字典内置原始数据映射对象
readonlyrawRaw字典原始数据
readonlysizenumber字典项数量

DictionaryBuiltinMethods<Data, Value, Key, Item>

字典内置方法

Extends

  • Pick<Item[], "forEach" | "filter" | "find" | "every" | "some">

Type parameters

ParameterValue
Dataunknown
Valueunknown
Key extends stringstring
Item extends DictionaryItemDictionaryItem<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
ParameterTypeDescription
predicate(value, index, array) => value is SA 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?anyAn 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
ParameterTypeDescription
predicate(value, index, array) => unknownA 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?anyAn 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
ParameterTypeDescription
predicate(value, index, array) => value is SA function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.
thisArg?anyAn 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
ParameterTypeDescription
predicate(value, index, array) => unknownA function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.
thisArg?anyAn 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
ParameterTypeDescription
predicate(value, index, obj) => value is Sfind 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?anyIf 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
ParameterType
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
ParameterTypeDescription
callbackfn(value, index, array) => voidA function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
thisArg?anyAn 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
ParameterTypeDescription
predicate(value, index, array) => unknownA 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?anyAn 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

PropertyTypeDescription
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

ParameterValue
Dataunknown
Valueunknown

Properties

PropertyTypeDescription
dataData字典项数据,来源于字典原始数据项
keystring字典项键
labelstring字典项标签
valueValue字典项值

DictionaryOptions<ValueKey, LabelKey, Key, Methods, Instance>

字典配置项

Type parameters

ParameterValue
ValueKey extends string-
LabelKey extends stringValueKey
Key extends stringValueKey
Methods extends Record<PropertyKey, AnyFn>Object
InstanceDictionary

Properties

PropertyTypeDescription
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

ParameterValue
Dataunknown
Valueunknown
Key extends stringstring
Methods extends Record<PropertyKey, AnyFn>Object
RawData[] | Record<string, Data>

DictionaryOrderByParams<T>

DictionaryOrderByParams<T>: _OrderByParams<T>

字典排序参数

Type parameters

ParameterValue
T extends DictionaryItem<any, any>DictionaryItem<any, any>