I have two enums Key & Label and an interface IOption and using enums i want create an Iptions object of array
const enum Key {
Flag = 'flag',
Checkbox = 'checkbox',
Star = 'star'
}
const enum Label {
Flag = 'Flag',
Checkbox = 'Checbox',
Star = 'Start'
}
interface IOption {
key: Key;
label: Label;
}
const dropDowOptions: Ioptions[]
what is the best way to achieve an array of IOption like this
const dropDownOptions = [
{myKey: Key.Flag, myLabel: Label.Flag },
{myKey: Key.Star, myLabel: Label.Flag},
{myKey: Key.CheckBox, myLael: Label.Checkbox}
]
I made this:
enum Key {
Flag = 'flag',
Checkbox = 'checkbox',
Star = 'star'
}
enum Label {
Flag = 'Flag',
Checkbox = 'Checbox',
Star = 'Start'
}
interface IOption {
key: Key;
label: Label;
}
var dropDownOptions: IOption[] = new Array<IOption>();
Object.keys(Key).forEach(key => {
dropDownOptions.push({
key: Key[key],
label: Label[key]
});
});
console.log(dropDownOptions);
Output:
[
{ key: 'flag', label: 'Flag' },
{ key: 'checkbox', label: 'Checbox' },
{ key: 'star', label: 'Start' }
]
If I've understood the question correctly you want to change the interface to describe the constant dropDownOptions
... you can do it like so
interface IOption {
myKey: Key;
myLabel: Label;
}
// what is the best way to achieve an array of IOption like this
const dropDownOptions: IOption[] = [
{ myKey: Key.Flag, myLabel: Label.Flag },
{ myKey: Key.Star, myLabel: Label.Flag },
{ myKey: Key.Checkbox, myLabel: Label.Checkbox }
]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.