How To Access An Enum By Index In TypeScript

Access an Enum by Index in TypeScript

One of the common and basic problems when working with enums is how to access an enum by index in TypeScript. If you are still wondering how to do so then please follow our instructions below.

Access an enum by index in TypeScript 

Enum is a kind of ‘class’ that allows us to create a group of constant values. There are three basic types of enum: numeric, string, and heterogeneous.

Normally, we will access an enum like this:

enum Jobs {
    Alex = 'teacher',
    Joey = 'doctor',
    Tom = 'bodybuilder',
    Iker = 'engineer',
    Albert = 'worker',
    David = 'chef',
    Emma = 'photographer',
    Mike = 'waiter',
    Noah = 'driver',
};

console.log(Jobs.David);
console.log(Jobs.Albert);
console.log(Jobs.Noah);
console.log(Jobs.Tom);

Output:

chef
worker
driver 
bodybuilder 

However, what if we want to access an enum using the index? Here are some possible solutions: 

Using the Object.values() method

The Object.values() method returns an array with all the values of the object. 

Syntax

Object.values(obj)

Parameters

  • obj: the object in which its values will be returned.

Return value

An array that contains all the values of the object.

For example: 

enum Jobs {
    Alex = 'teacher',
    Joey = 'doctor',
    Tom = 'bodybuilder',
    Iker = 'engineer',
    Albert = 'worker',
    David = 'chef',
    Emma = 'photographer',
    Mike = 'waiter',
    Noah = 'driver',
};

var myArr = Object.values(Jobs); //return an array containing all the values
console.log(myArr);

Output

["teacher", "doctor", "bodybuilder", "engineer", "worker", "chef", "photographer", "waiter", "driver"] 

Now, since this is an array, we can easily access it with an index, for example: 

var result = Object.values(Jobs)[4];

Completed code:

enum Jobs {
    Alex = 'teacher',
    Joey = 'doctor',
    Tom = 'bodybuilder',
    Iker = 'engineer',
    Albert = 'worker',
    David = 'chef',
    Emma = 'photographer',
    Mike = 'waiter',
    Noah = 'driver',
};

 // get the value of the fifth property
var result_1 = Object.values(Jobs)[4];
console.log(result_1);

// get the values of the seventh property
var result_2 = Object.values(Jobs)[6];
console.log(result_2);

Output:

worker 
photographer 

Using the Object.keys() method 

If you want to get the keys instead of the values, you can use the Object.keys() method. The Object.keys() works quite the same as the Object.values() but it will return the keys instead of the values. 

Syntax

Object.keys(obj)

Parameters: 

  • obj: the object in which its enumerable property names will be returned.

Return value

An array that contains enumerable properties of the object. 

Let’s apply it to our problem: 

enum Jobs {
    Alex = 'teacher',
    Joey = 'doctor',
    Tom = 'bodybuilder',
    Iker = 'engineer',
    Albert = 'worker',
    David = 'chef',
    Emma = 'photographer',
    Mike = 'waiter',
    Noah = 'driver',
};

// get the keys of the fifth property
var result_1 = Object.keys(Jobs)[4]; 
console.log(result_1);

// get the keys of the seventh property
var result_2 = Object.keys(Jobs)[6];
console.log(result_2);

Output:

Albert
Emma

Summary

In this tutorial, we have demonstrated two different ways to access an enum by Index in TypeScript. The idea is to use the Object.values() or the Object.keys() method. Let’s try these methods to get the desired results.

Maybe you are interested:

Leave a Reply

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