stackoverflow_user
stackoverflow_user

Reputation: 289

Assign same value to different keys in object literal reactjs

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.

Upvotes: 3

Views: 705

Answers (3)

r g
r g

Reputation: 3901

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

Upvotes: 1

ManavM
ManavM

Reputation: 3098

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.

Upvotes: 1

CROSP
CROSP

Reputation: 4617

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.

Upvotes: 1

Related Questions