How to get enum values as an array in Typescript

Get Enum values as an Array in TypeScript

To get enum values as an array in Typescript, you can use Object.values. So what is the detail? Let’s read this article now.

Get enum values as an array in TypeScript

Use the Object.values

Because an enum is an object behind the scenes and exits in the runtime, we can use the Object method.

Example:

enum choiceList {
    id1 = 'James',
    id2 = 'Tony',
    id3 = 'Anna',
    id4 = 'Thomas',
    id5 = 'Angel',
    id6 = 'Talor',
    id7 = 'Machel',
    id8 = 'Nanny'
}

Convert to JS:

"use strict";
var choiceList;
(function (choiceList) {
    choiceList["id1"] = "James";
    choiceList["id2"] = "Tony";
    choiceList["id3"] = "Anna";
    choiceList["id4"] = "Thomas";
    choiceList["id5"] = "Angel";
    choiceList["id6"] = "Talor";
    choiceList["id7"] = "Machel";
    choiceList["id8"] = "Nanny";
})(choiceList || (choiceList = {}));

You can use the Object.values method to get enum values as an array. This method will return an array of the enum’s values. 

Syntax:

Obenumsenumsvalues

Example:

enum choiceList {
    id1 = 'James',
    id2 = 'Tony',
    id3 = 'Anna',
    id4 = 'Thomas',
    id5 = 'Angel',
    id6 = 'Talor',
    id7 = 'Machel',
    id8 = 'Nanny'
}
 
// Get values array
const nameArr = Object.values(choiceList)
 
console.log(nameArr)

Output:

[LOG]: ["James", "Tony", "Anna", "Thomas", "Angel", "Talor", "Machel", "Nanny"] 

As you see here with the Object.values method, I can get all values in the choiceList enum. 

Use the Object.keys

You can also get a key array with the Object.keys method.

Example:

enum choiceList {
    id1 = 'James',
    id2 = 'Tony',
    id3 = 'Anna',
    id4 = 'Thomas',
    id5 = 'Angel',
    id6 = 'Talor',
    id7 = 'Machel',
    id8 = 'Nanny'
}
 
const keysArr = Object.keys(choiceList)
 
console.log(keysArr)

Output:

[LOG]: ["id1", "id2", "id3", "id4", "id5", "id6", "id7", "id8"] 

If you do not pass value in the enum and use the Object.values, an array containing number index and keys will be returned.

Example:

enum choiceList {
  id1,
  id2,
  id3,
  id4,
  id5,
  id6,
  id7,
  id8
}
 
const keysArr = Object.keys(choiceList)
 
console.log(keysArr)

Output:

[LOG]: ["0", "1", "2", "3", "4", "5", "6", "7", "id1", "id2", "id3", "id4", "id5", "id6", "id7", "id8"] 

After getting array values, you can work with it like an ordinary array. 

Example:

enum choiceList {
    id1 = 'James',
    id2 = 'Tony',
    id3 = 'Anna',
    id4 = 'Thomas',
    id5 = 'Angel',
    id6 = 'Talor',
    id7 = 'Machel',
    id8 = 'Nanny'
}
 
// Get values array
const nameArr = Object.values(choiceList)
 
for (let i=1 ; i< nameArr.length;i++) {
    console.log(`Choice ${i}: ${nameArr[i]}`)
}

Output:

[LOG]: "Choice 1: Tony"
[LOG]: "Choice 2: Anna"[LOG]: "Choice 3: Thomas"[LOG]: "Choice 4: Angel"[LOG]: "Choice 5: Talor"[LOG]: "Choice 6: Machel"[LOG]: "Choice 7: Nanny" 

Summary: 

In this article, I showed you how to get enum values as an array in Typescript. Because an enum is an object so you can use Object.values to get values array. Good luck for you!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *