How To Convert An Enum To An Array Of Objects In Typescript?

Convert an Enum to an Array of Objects in TypeScript

Converting an enum to an array of objects in Typescript will be very useful in situations like when you work with API. An array will be easier to control and work with. We have two simple methods to help you convert an enum to an array of Objects in Typescript? Let’s go into detail.

How to convert an enum to an array of objects in Typescript

To do it, first, you must know what an enum is in Typescript.
The enum will help you create a small data construct containing const values. Each value in the enum is known as a member of the enum. Enum is very useful when you want to set the property of a particular value.

Example:

enum petType {
    Dog,
    Cat,
    Turtle,
    Birds,
    Reptiles,
    AquariumPets
}

const onePetType = petType.AquariumPets;
console.log(onePetType);

Output:

5

If you look behind the scene, the Typescript code above will be compiled into Javascript code like this:

"use strict";
var petType;
(function (petType) {
    petType[petType["Dog"] = 0] = "Dog";
    petType[petType["Cat"] = 1] = "Cat";
    petType[petType["Turtle"] = 2] = "Turtle";
    petType[petType["Birds"] = 3] = "Birds";
    petType[petType["Reptiles"] = 4] = "Reptiles";
    petType[petType["AquariumPets"] = 5] = "AquariumPets";
})(petType || (petType = {}));
var onePetType = petType.AquariumPets;
console.log(onePetType);

You can understand easily that enum will create an object and then index it by the number from 0. It looks like this

{0:" Dog",1:" Cat",2:" Turtle",3:" Birds",4:" Reptiles",5:" AquariumPets"}

So we can apply it and convert it to an array with the object values method.

Use Object.values()

The Object.values() will return a new array in which the element is the Object’s value.

Syntax:

Object.values(obj)

Parameters:

  • obj: Object that you want to get values

So you can use Object. values method to convert an enum to an array in this way:

Example: 

enum petType {
    Dog,
    Cat,
    Turtle,
    Birds,
    Reptiles,
    AquariumPets
}

const convertedEnum = Object.values(petType);
console.log(convertedEnum);

Output:

["Dog", "Cat", "Turtle", "Birds", "Reptiles", "AquariumPets", 0, 1, 2, 3, 4, 5]

Here if you want to get the exact value that you indicate not with the index, you can do it this way:

Set default value to enum

enum petType {
    Dog = "Dog",
    Cat = "Cat",
    Turtle = "Turtle",
    Birds = "Birds",
    Reptiles = "Reptiles",
    AquariumPets = "AquariumPets"
}

const convertedEnum = Object.values(petType);
console.log(convertedEnum);

Output:

["Dog", "Cat", "Turtle", "Birds", "Reptiles", "AquariumPets"]

Summary

In this tutorial, I have explained how to convert an enum to an array of objects in Typescript. You can use Object.values to get an array of values of enum. If you have any problems, please comment below. I will answer as possible. Thanks for reading!

Maybe you are interested:

Leave a Reply

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