0

I define a simple enum like so:

export enum Type {
    TEST_ONE = "testing.one",
    TEST_TWO = "testing.two",
    BETA = "beta.one"
}

Now I want to execute a function for each enum string value. Let's say something like this:

executeType(type: string) { console.log(type) }

Object.keys(Type).forEach(
    type => {
        executeType(type);
    }
)

This outputs the enum values like TEST_ONE and BETA. How would I go about printing testing.one and beta.one. I've tried using type.toString() and type.valueOf().

1 Answer 1

1
Object.entries(Type).forEach(    
    (entry: [string, string]) => {   
        console.log(entry[1]);    
    }
)
Sign up to request clarification or add additional context in comments.

2 Comments

wow thanks! Not sure how I missed "entries". Seems so obvious now that I look at it, as I was iterating over "keys"
I believe it was easy to miss if you targeted es version less than es2017

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.