我想从枚举数据类型创建一个下拉列表,但是当我尝试将枚举更改为具有名称和值的对象时;枚举数组返回枚举成员的两倍。使用函数这个帖子 https://stackoverflow.com/questions/18111657/how-does-one-get-the-names-of-typescript-enum-entries/18112474#18112474
打字稿:v 2.3.3
Angular v 4.3.4
附:由于角度版本,无法更新打字稿版本。下面是相关代码
export enum FrequencyEnum {
H1 = <any>'1 Hour',
H12 = <any>'12 Hours',
D1 = <any>'24 Hours',
D7 = <any>'7 Days',
M1 = <any>'1 Month',
M3 = <any>'3 Months',
CD = <any>'Custom Date Range'
};
export interface Frequency {
name: string;
value: string;
range?: { from: string; to: string; };
}
export function enumSelector(definition) {
console.log('definition: ', definition);
return Object.keys(definition)
.map(key => ({ value: definition[key], name: key }));
}
this.frequnecyList = enumSelector(FrequencyEnum);
它给出了 14 个对象的数组
[
{
"value": "1 Hour",
"name": "H1"
},
{
"value": "H1",
"name": "1 Hour"
},
{
"value": "12 Hours",
"name": "H12"
},
{
"value": "H12",
"name": "12 Hours"
},
{
"value": "24 Hours",
"name": "D1"
},
{
"value": "D1",
"name": "24 Hours"
},
{
"value": "7 Days",
"name": "D7"
},
{
"value": "D7",
"name": "7 Days"
},
{
"value": "1 Month",
"name": "M1"
},
{
"value": "M1",
"name": "1 Month"
},
{
"value": "3 Months",
"name": "M3"
},
{
"value": "M3",
"name": "3 Months"
},
{
"value": "Custom Date Range",
"name": "CD"
},
{
"value": "CD",
"name": "Custom Date Range"
}
]
我怎样才能在最终数组中只得到 7 个值(等于枚举成员)?