AleX_
AleX_

Reputation: 508

How to get the name and props of global Objects in Javascript

I understand that variable names are not intrinsic properties of an object and thus cannot be retrieved when passed through functions. But here is my use-case and I'd like to create a mapping between members of UniversityEnums and displayStrings to get the display string.

const UniversityEnums = {
studentStatus: {Enrolled: 'Enrolled', OnHold: 'OnHold', Expelled: 'Expelled'},
professorStatus: {FullTime: 'FullTime', PartTime: 'PartTime', Emeritus: 'Emeritus', Expelled: 'Expelled'}
};

and

const displayStrings = {
studentStatus_Enrolled: 'Student is enrolled in the program',
studentStatus_OnHold: 'Student decided not to participate',
studentStatus_Expelled: 'Student was expelled',
professorStatus_FullTime: 'Staff member is hired fulltime',
professorStatus_PartTime: 'Staff member is hired parttime',
professorStatus_Emeritus: 'Staff member is retired',
professorStatus_Expelled: 'Staff member was expelled'};

My goal is to write a function that grabs a member of UniversityEnums and returns the corresponding display string, for example:

const expelledStudentDispStr = getDispString(UniversityEnums.studentStatus.Expelled);
console.log(expelledStudentDispStr);
// Student was expelled

The code I currently have has two input arguments and is like const expelledStudentDispStr = getDispString('studentStatus', UniversityEnums.studentStatus.Expelled); which needs the name of the enum to get the value but I am looking for an even smarter way!

Thanks in advance.

Note: that I can manipulate the enum object (for example define it with different variable names (or maybe, maybe, append other properties to it or its children). However, I CANNOT change their values because those values are used to compare those statuses against other variables. Also, the displayStrings is coming from a third party source and modifying them for me is not doable (at least easily!)

Possible workaround: One way that comes to my mind is to modify my enum object to have names that match displayStrings keys:

const UniversityEnums = {
studentStatus: {
    studentStatus_Enrolled: 'Enrolled',
    studentStatus_OnHold: 'OnHold',
    studentStatus_Expelled: 'Expelled'},
professorStatus: {
    professorStatus_FullTime: 'FullTime',
    professorStatus_PartTime: 'PartTime',
    professorStatus_Emeritus: 'Emeritus',
    professorStatus_Expelled: 'Expelled'}
};

Upvotes: 0

Views: 89

Answers (2)

Damien
Damien

Reputation: 633

Your

getDispString(UniversityEnums.studentStatus.Expelled)

is the same as

getDispString("Expelled")

and it would still be the same with your new object as

getDispString(UniversityEnums.studentStatus.studentStatus_Expelled)

you don't give it more information, on the other hand you could transform your enum like that

const UniversityEnums = {
  studentStatus: {
    Enrolled: { 
      status: 'Enrolled',
      entity: 'student'
    },
    OnHold: { 
      status: 'OnHold',
      entity: 'student'
    },
    Expelled: { 
      status: 'Expelled',
      entity: 'student'
    }
  }
};

so you would give the extra information you need

you could do something like

for (const kind in UniversityEnums)
  for (const value in UniversityEnums[kind])
    UniversityEnums[kind][value] = {
       kind: kind,
       value: value
    }

from Bergi's answer

Upvotes: 1

Bergi
Bergi

Reputation: 665256

Note that I can manipulate the enum object

In that case, it's easy: just put unique values in the enums so that you can distinguish them properly. For example,

for (const kind in UniversityEnums)
  for (const value in UniversityEnums[kind])
    UniversityEnums[kind][value] = kind + '_' + value;

With that you can write

function getDispString(enumValue) {
  return displayStrings[enumValue];
}

Upvotes: 1

Related Questions