开发者社区> 问答> 正文

Typescript:如何遍历枚举值以显示在单选按钮中?

在Typescript中遍历枚举枚举的正确方法是什么?(当前使用typescrip 1.8.1)

我有以下枚举:

export enum MotifIntervention { Intrusion, Identification, AbsenceTest, Autre }

export class InterventionDetails implements OnInit { constructor( private interService: InterventionService ) { let i:number = 0; for (let motif in MotifIntervention) { console.log( motif ); } } 显示的结果是一个列表

0 1 2 3 Intrusion, Identification, AbsenceTest, Autre 我确实只希望循环中有4次迭代,因为枚举中只有4个元素,我不想让0 1 2和3似乎是枚举的索引号。

问题来源于stack overflow

展开
收起
保持可爱mmm 2020-02-07 00:41:21 2068 0
1 条回答
写回答
取消 提交回答
  • 两种选择:

    for (let item in MotifIntervention) { if (isNaN(Number(item))) { console.log(item); } } 要么

    Object.keys(MotifIntervention).filter(key => !isNaN(Number(MotifIntervention[key])));

    2020-02-07 00:41:42
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
现代TypeScript高级教程 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载