3

i am using object literal as an alternative to if else/switch statements. In doing so not knowing how to assign same value to different keys.

What i am trying to do? Based on variable named "user" should redirect to different links. if value of "user" is admin or manager should redirect to say "www.exampledomain.com". If value of "user" is "purchaser" should redirect to "https://stackoverflow.com".

To do so i have used object literal instead of if-else which is clumsy. Below is the code,

get user()  {
    return ( {
        'admin': 'www.exampledomain.com',
        'manager': 'www.exampledomain.com',
        'purchaser': 'https://stackoverflow.com',
    } )[user];}

As you see from above code, admin and manager keys point to same url "www.exampledomain.com". Is there a way to assign it something like below.

get user()  {
    return ( {
        'admin': 'manager': www.exampledomain.com',
        'purchaser': 'https://stackoverflow.com',
    } )[user];}

Could somebody help me solving this. Thanks.

3 Answers 3

1

Personally, I don't see any reason to use the second idea, furthermoe it is not valid JS code. If you are trying to reduce code duplication you can just extract your urls into a constant variable like that.

  static get REDIRECT_URLS() {
    return { 
       "PRIMARY_SITE" : "www.exampledomain.com",
       "SECONDARY_SITE" : "stackoverflow.com",
    };
  }

  get user()  {
    return ( {
        'manager' : FooClass.REDIRECT_URLS.PRIMARY_SITE,
        'admin': FooClass.REDIRECT_URLS.PRIMARY_SITE,
        'purchaser': FooClass.REDIRECT_URLS.SECONDARY_SITE,
    } )[user];}

Of course there are other possible solutions, like having keys like 'admin|manager' : "url", but that doesn't seem to be a good choice and you need to add extra logic to iterate over the object keys and check if a key matched the regex.

Sign up to request clarification or add additional context in comments.

5 Comments

@ManavM as I noted, there should be additional logic for matching object keys and that doesn't make any sense
So basically just some method where you would iterate over this object and have some logic to extract the requisite data from it? I suppose that would work too...I tried to make an implementation that is similar to something like that in my answer below but it doesn't feel clean.
@ManavM it could implemented in different ways, for example use a closure to capture context with constant values and logic for iterating over the object keys. However I strongly don't suggest to go in that way. Your answer is another way, you have inverted properties, but in that case it violates some readability concepts, this could be read as "assign user roles to url"
I agree..Well, thank you for taking the time to further explain your answer.
@ManavM, you are welcome, it is really good that you are trying find better solutions!
1

If the problem were to be viewed in isolation, I would solve this issue by simply flipping the data structure around

const linkToTypeMapping = {
  'www.exampledomain.com': ['admin', 'manager'],
  'https://stackoverflow.com': ['purchaser'],
}

But that doesn't really fix your issue.

The way I would solve the actual use-case is to simply add a link property to your user object and just populate and later access userObject.link.

However for completeness's sake, here's how you would extract a user's link from the data structure I posted above.

const get_link = (type) => {
  for (let key in linkToTypeMapping) {
    if(linkToTypeMapping.includes(type)) {
      return key;
    }
  }
}

This is obviously very complicated as far as the code goes, but if your linkToTypeMapping object is expected to become fairly large, this might actually be the right solution for you.

Comments

1

You can make default route like that:

function user() {
  const defaultRoute = "www.exampledomain.com"
  return ({
    'admin': defaultRoute,
    'manager': defaultRoute,
    'purchaser': 'https://stackoverflow.com'
  })[user];
}

or that

function user2() {
  const defaultRoute = "www.exampledomain.com"
  return ({
    'someoneElse': "www.google.com",
    'purchaser': 'https://stackoverflow.com'
  })[user] || defaultRoute;
}

or mix both styles, depending on how complex your statement is;

or even make it from the other side

function user3(user) {
  const routes = {
    "www.google.com": ["admin", "manager"],
    "www.exampledomain.com": ["purchaser"]
  };

  return Object.keys(routes).find(k => routes[k].includes(user));
}

console.log(user3('manager')); //www.google.com

Comments

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.