Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert enum into a key,value array in typescript?

var enums = {
  '1': 'HELLO',
  '2' : 'BYE',
  '3' : 'TATA'
  };

I want to be able to convert that into an array that looks like this,

[
  {
    number:'1',
    word:'HELLO'
  },
  {
    number:'2',
    word:'BYE'
  },
  {
    number:'3',
    word:'TATA'
  }
]

all of the solutions I see form an array of either the keys or the values.

like image 575
user7434041 Avatar asked Jun 06 '26 01:06

user7434041


1 Answers

You could map the entries with short hand properties.

var enums = { 1: 'HELLO', 2: 'BYE', 3: 'TATA' },
    objects = Object.entries(enums).map(([number, word]) => ({ number, word }));

console.log(objects);
.as-console-wrapper { max-height: 100% !important; top: 0; }
like image 138
Nina Scholz Avatar answered Jun 08 '26 14:06

Nina Scholz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!