TypeScript Enum | How to Convert Enum into an Array in Typescript

Enums are a set of named constants in TypeScript. Enums are useful when you have a fixed set of constants. Sometimes we need to  Convert an enum into an array in Typescript, and this can be quite easy.

In this article, we will learn How to Convert an enum into an array in Typescript with some examples.

Let’s get started,

Enums As we have discussed, define a set of named constants that exist at runtime as real objects.

TypeScript Enum

Convert numeric enum into an array

To convert a numeric enum into an array we need to use the “Object.values” with the filter function

Let’s have an example

enum gender{
    Male,
    Female,
    Others
}
console.log(Object.values(gender));

When we run this example we will get this result.

> tsc enumtoarray.ts 
> node enumtoarray.js

[ 'Male', 'Female', 'Others', 0, 1, 2 ]

Suppose if we need to filter out only the numeric value then we have to use the filter function.

Like so,

enum gender{
    Male,
    Female,
    Others
}
console.log(Object.values(gender).filter(val=>isNaN(Number(val))));
 

If we need to filter out only the string value.

enum gender{
    Male,
    Female,
    Others
}

console.log(Object.values(gender).filter(val=>!isNaN(Number(val))));

Here is the result:

[ 'Male', 'Female', 'Others' ]
[ 0, 1, 2 ]

Convert a string enum into an array

To convert a string enum into an array we have to use the Object.values or  Object.key functions

Let’s have an example

enum department{
    CS = 'Computer Science Engg',
    Mech = 'Mechanical Engineering',
    EC = 'Electronic and Communication Engg',
    CV = 'Civil Engineering'
}

console.log(Object.keys(department));
console.log(Object.values(department));

Here is the output:

[ 'CS', 'Mech', 'EC', 'CV' ]
[
  'Computer Science Engg',
  'Mechanical Engineering',
  'Electronic and Communication Engg',
  'Civil Engineering'
]

Converting an Enums Key-value pair into an array

To transform an enum into an array of key-value pairs, we need to use the Object.entries() Method with applying a filter function to remove any pairs where the value is not a number.

const Gendernum : [string,number][]=Object.entries(gender)
.filter(([key,value])=> typeof value === 'number')
.map(([key,value])=> [key,value as number]);

console.log(Gendernum);

Result:

[ [ 'Male', 0 ], [ 'Female', 1 ], [ 'Others', 2 ] ]

enum department{
    CS = 'Computer Science Engg',
    Mech = 'Mechanical Engineering',
    EC = 'Electronic and Communication Engg',
    CV = 'Civil Engineering'
}
export const gendermodel:{
    value: string;
    key : string;
}[]=Object.entries(department)
.map(([key,value])=>({key,value}));

console.log(gendermodel);

Result:

[
  { key: 'CS', value: 'Computer Science Engg' },
  { key: 'Mech', value: 'Mechanical Engineering' },
  { key: 'EC', value: 'Electronic and Communication Engg' },
  { key: 'CV', value: 'Civil Engineering' }
]

More Articles – How to Create a Multi-Select Dropdown List in Angular

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments