简体   繁体   中英

More efficient way than using lots of else if statements

I'm trying to find a better way to do this in Javascript:

if ( text === 'amy' ) {
var url = 'http://www.mydomain.com/amylikescats.html';
}
else if ( text === 'dave' ) {
var url = 'http://www.mydomain.com/daveshome.html';
}
else if ( text === 'steve' ) {
var url = 'http://www.mydomain.com/steve2.html';
}
else if ( text === 'jake' ) {
var url = 'http://www.mydomain.com/jakeeatstofu.html';
}
else {
var url = 'http://www.mydomain.com/noone.html';
}

Is there a more code efficient way of doing this?'

Use an object as a map:

var map = {
    "amy": 'http://www.mydomain.com/amylikescats.html',
    "dave": 'http://www.mydomain.com/daveshome.html',
    // etc
};

var text = "whatever";
var url = map[text] === undefined ? 'http://www.mydomain.com/noone.html' : map[text];

This will save you the maximum amount of repeated code, but if you also need to do other stuff than setting url a switch might be more appropriate.

Switch statement!

var url = 'http://www.mydomain.com/noone.html';
switch(text) {
  case 'amy': url = 'http://www.mydomain.com/amylikescats.html';
  break;
  case 'dave': url = 'http://www.mydomain.com/daveshome.html';
  break;
  case 'steve': url = 'http://www.mydomain.com/steve2.html';
  break;
  case 'jake': url = 'http://www.mydomain.com/jakeeatstofu.html';
  break;
}

Now there is no need for a default clause because you've initialized url before the switch.

Otherwise you could add this:

default: url = 'http://www.mydomain.com/noone.html';
break;

Associative array:

var data = {
  amy: 'http://www.mydomain.com/amylikescats.html',
  dave: 'http://www.mydomain.com/daveshome.html',
  // etc... 
}

To use:

var url = data[text];

The else case can be replicate dby the non-existance of the item in the array, so expanding a bit:

var url = '';
if(!(text in data)){
    url = 'http://www.mydomain.com/daveshome.html';
}
else{
    url = data[text];
}

Store the unique parts in a dictionary and then take it from there:

var map = {
    amy: "amylikescats",
    dave: "daveshome",
    steve: "steve2",
    jake: "jakeeatstofu"
};
var url = map[text];
if (!url) {
    url = 'http://www.mydomain.com/noone.html';
} else {
    url = 'http://www.mydomain.com/' + url + '.html';
}

You could use an object to hold the URLs for different values of text, and then use the || operator when assigning a value to url to use the fallback value if necessary.

var urlsForText = {
      'amy': 'http://www.mydomain.com/amylikescats.html',
     'dave': 'http://www.mydomain.com/daveshome.html',
    'steve': 'http://www.mydomain.com/steve2.html',
     'jake': 'http://www.mydomain.com/jakeeatstofu.html'
};

var url = urlsForText[text] || 'http://www.mydomain.com/noone.html';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM