0

I have an enumeration defined like this:

module.exports = {
    APP_TYPES: {
        TYPE_ONE: { id: 5, name: "Application One name"},
        TYPE_TWO: { id: 9, name: "Application Two name"},
        TYPE_THREE: { id: 6, name: "Application Three name"}
    }
}

I want to be able to reverse lookup the enum based on property values.

   lookupById: function(id) {
       for (var app in this.APP_TYPES) {
           if(this.APP_TYPES.hasOwnProperty(app) && app.id === id) {
                return app;
            }
       }
    }

It appears that I cannot access the 'id' property of the enums. How do I refactor this so that I can access the properties defined on the enums?

Selena
  • 2,208
  • 8
  • 29
  • 49
  • 1
    you can access the property just fine, you just need to do `this.APP_TYPES[app].id` rather than just `app.id`. I'd also point out that what you're dealing with here is not an enum, but rather a simple javascript Object – Hamms May 22 '17 at 22:41
  • Hamms is right. `app` is `TYPE_ONE`, etc., rather than `{ id: 5, name: "Application One name"}`. – X. Liu May 22 '17 at 22:46

1 Answers1

0

Since app is the key on the APP_TYPES object, you have to access APP_TYPES[app] to get the object which has the id property

lookupById: function(id) {
    for (var app in this.APP_TYPES) {
        if(this.APP_TYPES.hasOwnProperty(app) && this.APP_TYPES[app].id === id) {
             return app;
         }
    }
 }
xdumaine
  • 10,096
  • 6
  • 62
  • 103