Skip to content

Functions ​

toDictionary() ​

支持对象和数组转为字典

Param ​

对象或数组

Param ​

配置项

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' } }

// 数组转字典
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 ParameterDefault type
Data-
ValueKey extends string"value"
LabelKey extends stringValueKey
Key extends stringValueKey
ValueData extends Primitive ? Data<Data> : Data extends Record<ValueKey, Value> ? Value : unknown
DataKey extends stringData extends Primitive ? Data<Data> extends string ? string & Data<Data> : string : string
Methods extends Record<PropertyKey, Fn>object
Parameters ​
ParameterTypeDescription
arrayData[]数组
options?DictionaryOptions<ValueKey, LabelKey, Key, Methods, Dictionary<Data, Value, DataKey, Methods, Data[]>>配置项
Returns ​

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

字典

Param ​

对象或数组

Param ​

配置项

Examples ​
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' } }

// 数组转字典
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' } }
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' } }

Call Signature ​

toDictionary<Raw, Data, ValueKey, LabelKey, Key, Value, DataKey, Methods>(object, options?): Dictionary<Data, Value, DataKey, Methods, Raw>

对象转字典

Type Parameters ​
Type ParameterDefault type
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> : Data extends Record<ValueKey, Value> ? Value : unknown
DataKey extends string | string & Record<never, never>KeyOf<Raw, string>
Methods extends Record<PropertyKey, Fn>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>

字典

Param ​

对象或数组

Param ​

配置项

Examples ​
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' } }

// 数组转字典
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' } }
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 ​

Type ParameterDefault type
Rawunknown
Dataunknown
Valueunknown

Properties ​

PropertyModifierTypeDescription
mapreadonlyMap<string, DictionaryItem<Data, Value>>字典内置原始数据映射对象
rawreadonlyRaw字典原始数据
sizereadonlynumber字典项数量

DictionaryBuiltinMethods<Data, Value, Key, Item> ​

字典内置方法

Extends ​

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

Type Parameters ​

Type ParameterDefault type
Dataunknown
Valueunknown
Key extends stringstring
Item extends DictionaryItemDictionaryItem<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 ​
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

Call Signature ​

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() ​
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 ​
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

Call Signature ​

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() ​
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 ​
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

Call Signature ​

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() => [string, Item][]获取字典键项对列表 Example const dict = toDictionary( { a: { id: 1, text: 'A' }, b: { id: 2, text: 'B' } }, { valueKey: 'id', labelKey: 'text' }, ) dict.entries() [ ['a', { key: 'a', value: 1, label: 'A', data: { id: 1, text: 'A' } }], ['b', { key: 'b', value: 2, label: 'B', data: { id: 2, text: 'B' } }], ]
eqValue<T>(key: T, value: unknown) => boolean比较指定字典键值,若指定字典键不存在则恒为 false Example const dict = toDictionary( { a: { id: 1, text: 'A' }, b: { id: 2, text: 'B' } }, { valueKey: 'id', labelKey: 'text' }, ) dict.eqValue('a', 1) // => true dict.eqValue('c', undefined) // => false
get<T>(key: T) => undefined | Item根据指定键获取字典项 Example const dict = toDictionary( { a: { id: 1, text: 'A' }, b: { id: 2, text: 'B' } }, { valueKey: 'id', labelKey: 'text' }, ) dict.get('a') // => { key: 'a', value: 1, label: 'A', data: { id: 1, text: 'A' } } dict.get('c') // => undefined
getByValue(value: unknown) => undefined | Item根据指定字典项值获取字典项 Example const dict = toDictionary( { a: { id: 1, text: 'A' }, b: { id: 2, text: 'B' } }, { valueKey: 'id', labelKey: 'text' }, ) dict.getByValue(1) // => { key: 'a', value: 1, label: 'A', data: { id: 1, text: 'A' } } dict.getByValue(3) // => undefined
getData<T>(key: T) => undefined | Item["data"]根据指定键获取字典项数据 Example const dict = toDictionary( { a: { id: 1, text: 'A' }, b: { id: 2, text: 'B' } }, { valueKey: 'id', labelKey: 'text' }, ) dict.getData('a') // => { id: 1, text: 'A' } dict.getData('c') // => undefined
getDataByValue(value: unknown) => undefined | Item["data"]根据指定字典项值获取字典项数据 Example const dict = toDictionary( { a: { id: 1, text: 'A' }, b: { id: 2, text: 'B' } }, { valueKey: 'id', labelKey: 'text' }, ) dict.getDataByValue(1) // => { id: 1, text: 'A' } dict.getDataByValue(3) // => undefined
getKey<T>(key: T) => string根据指定键获取字典项键,用于明确字面量意义 Example const dict = toDictionary( { a: { id: 1, text: 'A' }, b: { id: 2, text: 'B' } }, { valueKey: 'id', labelKey: 'text' }, ) dict.getKey('a') // => 'a' dict.getKey('c') // => '' const list = [ // 字面量 'a' 与 dict 绑定不明显,不易于修改 { name: 'a', label: dict.getLabel('a'), }, // 字面量 'b' 与 dict 强关联,易于修改 { name: dict.getKey('b'), label: dict.getLabel('b'), }, ]
getKeyByValue(value: unknown) => string根据指定字典项值获取字典项键 Example const dict = toDictionary( { a: { id: 1, text: 'A' }, b: { id: 2, text: 'B' } }, { valueKey: 'id', labelKey: 'text' }, ) dict.getKeyByValue(1) // => 'a' dict.getKeyByValue(3) // => ''
getLabel<T>(key: T) => string根据指定键获取字典项标签 Example const dict = toDictionary( { a: { id: 1, text: 'A' }, b: { id: 2, text: 'B' } }, { valueKey: 'id', labelKey: 'text' }, ) dict.getLabel('a') // => 'A' dict.getLabel('c') // => ''
getLabelByValue(value: unknown) => string根据指定字典项值获取字典项标签 Example const dict = toDictionary( { a: { id: 1, text: 'A' }, b: { id: 2, text: 'B' } }, { valueKey: 'id', labelKey: 'text' }, ) dict.getLabelByValue(1) // => 'A' dict.getLabelByValue(3) // => ''
getValue<V, T>(key: T) => undefined | V根据指定键获取字典项值 Example const dict = toDictionary( { a: { id: 1, text: 'A' }, b: { id: 2, text: 'B' } }, { valueKey: 'id', labelKey: 'text' }, ) dict.getValue('a') // => 1 dict.getValue('c') // => undefined
has<T>(key: T) => boolean根据指定键判断是否存在字典项 Example const dict = toDictionary( { a: { id: 1, text: 'A' }, b: { id: 2, text: 'B' } }, { valueKey: 'id', labelKey: 'text' }, ) dict.has('a') // => true dict.has('c') // => false
items() => Item[]获取字典项列表 Example const dict = toDictionary( { a: { id: 1, text: 'A' }, b: { id: 2, text: 'B' } }, { valueKey: 'id', labelKey: 'text' }, ) dict.items() [ { key: 'a', value: 1, label: 'A', data: { id: 1, text: 'A' } }, { key: 'b', value: 2, label: 'B', data: { id: 2, text: 'B' } }, ]
keys() => string[]获取字典键列表 Example const dict = toDictionary( { a: { id: 1, text: 'A' }, b: { id: 2, text: 'B' } }, { valueKey: 'id', labelKey: 'text' }, ) dict.keys() ['a', 'b']
labels() => string[]获取字典项标签列表 Examples `` const dict = toDictionary( { a: { id: 1, text: 'A' }, b: { id: 2, text: 'B' } }, { valueKey: 'id', labelKey: 'text' }, ) dict.labels() ['A', 'B']
values<T>() => T[]获取字典项值列表 Example const dict = toDictionary( { a: { id: 1, text: 'A' }, b: { id: 2, text: 'B' } }, { valueKey: 'id', labelKey: 'text' }, ) dict.values() [1, 2]

DictionaryItem<Data, Value> ​

字典项

Type Parameters ​

Type ParameterDefault type
Dataunknown
Valueunknown

Properties ​

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

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

字典配置项

Type Parameters ​

Type ParameterDefault type
ValueKey extends string-
LabelKey extends stringValueKey
Key extends stringValueKey
Methods extends Record<PropertyKey, Fn>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 ​

Type ParameterDefault type
Dataunknown
Valueunknown
Key extends stringstring
Methods extends Record<PropertyKey, Fn>object
RawData[] | Record<string, Data>