Enumerate an enum in JavaScript
This post will discuss how to enumerate an enum in JavaScript.
Enumerating an enum in JavaScript means getting the names and values of the enum members. An enum is a way of defining a set of named constants that represent some category or type. JavaScript does not have a native support for enums, but there are several ways to simulate them using objects, functions, or classes. There are several ways to enumerate an enum in JavaScript, depending on how the enum is defined. Here are some of the methods that we can use to enumerate an enum in JavaScript:
1. Using a function
One way is to use a function that takes a list of arguments and assigns them as properties of an object with the same name and value. Then, we can use Object.keys() to get an array of the enum names, and use a loop or a map function to get the corresponding values. For example, this code will create an enum for operating systems and enumerate its members:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
function Enum(){ for(let i = 0; i < arguments.length; ++i){ this[arguments[i]] = i; } return this; } const OS = new Enum('Linux', 'MacOS', 'Windows', 'Ubuntu'); // Get the keys let keys = Object.keys(OS); console.log(keys); // ['Linux', 'MacOS', 'Windows', 'Ubuntu'] for (let key of keys) { console.log(`${key}: ${OS[key]}`); // Linux: 0, MacOS: 1, Windows: 2, etc. } // or keys.forEach(function(key) { console.log(`${key}: ${OS[key]}`); // Linux: 0, MacOS: 1, Windows: 2, etc. }); |
2. Using an object literal
Another way is to use an object literal that defines the enum members as properties with numeric or string values. Then, we can use Object.keys() to get an array of the keys of an object, and then use a for loop or a forEach() function to iterate over them. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
const Direction = { North: 0, South: 1, East: 2, West: 3 }; // Get the keys let keys = Object.keys(Direction); console.log(keys); // ['North', 'South', 'East', 'West'] for (let key of keys) { let value = Direction[key]; console.log(`${key}: ${value}`); // North: 0, South: 1, East: 2, etc. } // or keys.forEach(function(key) { let value = Direction[key]; console.log(`${key}: ${value}`); // North: 0, South: 1, East: 2, etc. }); |
We can also use Object.entries() to get an array of the key-value pairs of an object, which can be used to iterate over the entries of an enum object using a for loop or a forEach() function. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
const Direction = { North: 0, South: 1, East: 2, West: 3 }; // Get the entries let entries = Object.entries(Direction); // [['North', 0], ['South', 1], ['East', 2], ['West', 3]] console.log(entries); for (let entry of entries) { let key = entry[0]; let value = entry[1]; console.log(`${key}: ${value}`); // North: 0, South: 1, East: 2, etc. } entries.forEach(function(entry) { let key = entry[0]; let value = entry[1]; console.log(`${key}: ${value}`); // North: 0, South: 1, East: 2, etc. }); |
That’s all about enumerating an enum in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)