gpt4 book ai didi

javascript - 遍历枚举,将枚举元素指定为参数

转载 作者:搜寻专家 更新时间:2023-10-30 21:13:41 25 4
gpt4 key购买 nike

我想迭代一个枚举。当我遍历这个枚举时,我得到了键和值,但我只想使用键将技能实例添加到技能数组。

enum eSkills {
ACROBATICS = <any>"Acrobatics",
APPRAISE = <any>"Appraise",
BLUFF = <any>"Bluff",
CLIMB = <any>"Climb",
CRAFT = <any>"Craft"
}

class Skill {
constructor(name: eSkills) {
this.name = name;
}

name: eSkills;
}

let skills: Skill[] = [];

for (let skill in eSkills) {
//TODO create new instance of Skill and push to skills array
}

我需要枚举的反向映射。

最佳答案

我猜你已经试过了,但没有用:

skills.push(new Skill(skill)); // Error: Argument of type 'string' is not assignable to parameter of type 'eSkills'.

您可以使用 type assertion 来解决这个问题(作为 eSkills):

skills.push(new Skill(skill as eSkills));

demo

关于javascript - 遍历枚举,将枚举元素指定为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50543077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com