-1

I want to get a enum from object, in typescript, is it possible? Example below!



const keys = ['a', 'b', 'c', 'd', 'e']
const values = [1, 2, 3, 4, 5]


// expected result
enum result {
  a = 1,
  b = 2,
  c = 3,
  d = 4,
  e = 5
}


2
  • nope, these answers didn't answer to my question! Commented Nov 1, 2022 at 8:34
  • I already view them before creating a question! Commented Nov 1, 2022 at 8:34

1 Answer 1

1

No you can not get an enum, which is a pre-defined type.

What you are trying to get is a mapping between the two arrays (dictionary/object), you can create it dynamically if they are always in the correct order.

const keys = ['a', 'b', 'c', 'd', 'e']
const values = [1, 2, 3, 4, 5]

// mapping
let dictionary  =  {};
keys.forEach((key, i)  => dictionary[key] = values[i]);
console.log(dictionary);

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

3 Comments

yep, I already do that, get key:value pair object from array, but I want to know, is there any possibility to get enum from object or build enum dynamically
@GevorgMartirosyan Doesn't "No you can not get an enum, which is a pre-defined type" answer your question? TypeScript is compiled into JavaScript. At runtime (which is where the "dynamic" code runs), enums don't exist. But this feels like an XY problem.
yeah, you already answer, thank you a lot!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.