How to get all enum names as an array in Typescript

Get all Enum Names as an Array in TypeScript

To get all enum names as an array in Typescript, you can use the Object.keys method. So what is the detail? Let’s go into detail by reading this article.

Get all enum names as an array in TypeScript

Use Object.keys

First of all, an enum is an object behind the scenes in runtime.

Example:

enum choiceList {
    animal1 = 'Atlantic Spotted Dolphins',
    animal2 = 'British Wild Cats',
    animal3 = 'Common Hippopotamus',
    animal4 = 'Elephants',
    animal5 = 'European Otter',
    animal6 = 'Galapagos Fur Seals',
    animal7 = 'Galapagos Whale Shark',
    animal8= 'Turtles'
}
 

Convert to JS:

"use strict";
var choiceList;
(function (choiceList) {
    choiceList["animal1"] = "Atlantic Spotted Dolphins";
    choiceList["animal2"] = "British Wild Cats";
    choiceList["animal3"] = "Common Hippopotamus";
    choiceList["animal4"] = "Elephants";
    choiceList["animal5"] = "European Otter";
    choiceList["animal6"] = "Galapagos Fur Seals";
    choiceList["animal7"] = "Galapagos Whale Shark";
    choiceList["animal8"] = "Turtles";
})(choiceList || (choiceList = {}));

To get enum names as an array here, you can use the Object.keys method because we will have keys and values pair in Object type.

Example:

enum choiceList {
    animal1 = 'Atlantic Spotted Dolphins',
    animal2 = 'British Wild Cats',
    animal3 = 'Common Hippopotamus',
    animal4 = 'Elephants',
    animal5 = 'European Otter',
    animal6 = 'Galapagos Fur Seals',
    animal7 = 'Galapagos Whale Shark',
    animal8= 'Turtles'
}
 
const nameArr = Object.keys(choiceList)
 
console.log(nameArr)

Output:

[LOG]: ["animal1", "animal2", "animal3", "animal4", "animal5", "animal6", "animal7", "animal8"] 

Use Object.values 

Besides the names array, you can get a value array from the Object.values method.

Example:

enum choiceList {
    animal1 = 'Atlantic Spotted Dolphins',
    animal2 = 'British Wild Cats',
    animal3 = 'Common Hippopotamus',
    animal4 = 'Elephants',
    animal5 = 'European Otter',
    animal6 = 'Galapagos Fur Seals',
    animal7 = 'Galapagos Whale Shark',
    animal8= 'Turtles'
}
 
const valueArr = Object.values(choiceList)
 
console.log(valueArr)

Output:

[LOG]: ["Atlantic Spotted Dolphins", "British Wild Cats", "Common Hippopotamus", "Elephants", "European Otter", "Galapagos Fur Seals", "Galapagos Whale Shark", "Turtles"] 

If you do not pass in value in the enum, it will become a const enum. If you use Object.keys method, it will return the index value and name value simultaneously.

Example:

enum choiceList {
    animal1 ,
    animal2,
    animal3 ,
    animal4 ,
    animal5 ,
    animal6 ,
    animal7 ,
    animal8 ,
}
 
const nameArr= Object.keys(choiceList)
 
console.log(nameArr)

Output:

[LOG]: ["0", "1", "2", "3", "4", "5", "6", "7", "animal1", "animal2", "animal3", "animal4", "animal5", "animal6", "animal7", "animal8"] 

After getting all enum names as an array, you can work with it like an ordinary array.

Example:

enum choiceList {
    animal1 = 'Atlantic Spotted Dolphins',
    animal2 = 'British Wild Cats',
    animal3 = 'Common Hippopotamus',
    animal4 = 'Elephants',
    animal5 = 'European Otter',
    animal6 = 'Galapagos Fur Seals',
    animal7 = 'Galapagos Whale Shark',
    animal8= 'Turtles'
}
 
const nameArr= Object.keys(choiceList)
 
console.log(nameArr)
for (let i=1; i< nameArr.length; i++){
    console.log(nameArr[i])
}

Output:

[LOG]: "animal2"
[LOG]: "animal3"[LOG]: "animal4"[LOG]: "animal5"[LOG]: "animal6"[LOG]: "animal7"[LOG]: "animal8" 

You can read about object type here.

Summary

In this article, I showed you how to get all enum names as an array in Typescript. An enum is an Object behind the scene, so you can use the Object.keys method to get all names in the enum as an array. Good luck for you!

Maybe you are interested:

Leave a Reply

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