开发者社区 问答 正文

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 2196 分享 版权
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
    赞同 展开评论
问答分类:
问答地址: