0

I have one enum that I would like to do reverse mapping so eventually I have one object with key as enum.value and value as enum's key I can hardcode all key-value pair but how can I write something more dynamic?

export declare enum SAMPLE {
  AIR = 0,
  EARTH = 1,
  WATER = 2
}

REVERSESAMPLE {
SAMPLE.AIR = 'Air',
SAMPLE.EARTH = 'Earth',
SAMPLE.WATER = 'Water',

}

3
  • 1
    Numeric enums already have a reverse mapping, does that not suffice? If not, please consider editing the question to make it the issue explicit. Also note though that "AIR" and "Air" are different strings; is that case difference intentional or a typo? Are you intending to declare the enum instead of actually emitting it? If so, how does it work? Commented Mar 10, 2022 at 21:25
  • yes, it is intentional or different case and declare. so I have to to SAMPLE enum from other file and the based on the type value from the object {type: 1} I have i need to return the string 'Earth'. however for some reason the i get undefined from SAMPLE[Object.type] Commented Mar 10, 2022 at 23:09
  • I’m sorry, I really don’t understand that at all 😞. Your sentences don’t make sense to me. Could you try to rephrase? I imagine that you might not be a native speaker of English, but I can’t help if I don’t understand you. Commented Mar 10, 2022 at 23:36

1 Answer 1

1

You don't need to do this. Sample[0] => "Air" And Sample.Air => 0

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.