How to iterate strings,values of an Enum type in javascript/typescript


Enum is an enumeration of names and values replacing multiple constants with a single namespace. There are many ways we can iterate enum data.

This blog post covers the examples for looping the Enum string or numbers using different approaches

Using Object inbuilt methods iterate

An enum is also an object in javascript, Object class provides the following methods

  • The Object.keys() method returns array of the keys of an object
  • The Object.values() method returns an array of values or properties of an object
  • The Object.entries() method returns Multimap of an enumeration, It is introduced in latest javascript language

let us declare Enum in javascript or typescript

export enum Status {
    ACTIVE ="active",
    INACTIVE="inactive",
    PENDING ="pending",
    COMPLETED = "completed"

}

An example to retrieve keys of an Enum

const keys=Object.keys(Status);
console.log(keys)//[ 'ACTIVE', 'INACTIVE', 'PENDING', 'COMPLETED' ]

An example to retrieve keys of an Enum

const values=Object.values(Status);
console.log(values)//[ 'active', 'inactive', 'pending', 'completed' ]

iterate Multiple Map key and values of an enum

const entries=Object.entries(Status);
console.log(entries)

[ [ 'ACTIVE', 'active' ],
  [ 'INACTIVE', 'inactive' ],
  [ 'PENDING', 'pending' ],
  [ 'COMPLETED', 'completed' ] ]

using in operator with for loop

in operator in javascript, check property exists in an object and returns true Enum object holds the normal and reverses order of key and value pair

Each property is iterated and printed property name and its value using Enum[property]

for (let element in Status) {
        console.log(element +" - "+ Status[element]);
}

And the output is

ACTIVE - active
INACTIVE - inactive
PENDING - pending
COMPLETED - completed

forIn lodash method

forIn function in lodash used to iterate own enumerated properties of an object, Since enum is an object, used to iterate keys and values of  an enum Iterate each key and value pairs apply the call back for each iteration, It can take an object, callback value, key pairs.

_.forIn(Status, function(value, key) {
        console.log(key +" - "+ value]);
});

get of properties of an Enum with no value

In the above examples, Enum is declared with property and its value and checked various approaches to do iteration

Now we will see the list of strings of enum that has no values.

export enum Status {
    ACTIVE ,
    INACTIVE,
    PENDING,
    COMPLETED

}
console.log(Status);

And enum object printed the below format,

{ '0': 'ACTIVE',
  '1': 'INACTIVE',
  '2': 'PENDING',
  '3': 'COMPLETED',
  ACTIVE: 0,
  INACTIVE: 1,
  PENDING: 2,
  COMPLETED: 3 }

loop each element of an object  using for loop within the operator or object method check each element value is not a number and isNan method.

const statuslist = Object.keys(Status).filter((element) => {
    return isNaN(Number(element));
});
console.log(statuslist);

for (let element in Status) {
    if (isNaN(Number(element))) {
        console.log(element);
    }
}

And output

[ 'ACTIVE', 'INACTIVE', 'PENDING', 'COMPLETED' ]
ACTIVE
INACTIVE
PENDING
COMPLETED
Similar Posts