最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - More efficient way than using lots of else if statements - Stack Overflow

programmeradmin3浏览0评论

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

if ( text === 'amy' ) {
var url = '.html';
}
else if ( text === 'dave' ) {
var url = '.html';
}
else if ( text === 'steve' ) {
var url = '.html';
}
else if ( text === 'jake' ) {
var url = '.html';
}
else {
var url = '.html';
}

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

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

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

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

Share Improve this question asked Aug 7, 2012 at 10:49 Cheeky CherubCheeky Cherub 1331 gold badge2 silver badges7 bronze badges 1
  • 2 There are already some great answers below - this other stack overflow question is quite well referenced too and explains in a bit more depth: stackoverflow./a/2573153/649979 – Graham Smith Commented Aug 7, 2012 at 10:53
Add a ment  | 

5 Answers 5

Reset to default 14

Use an object as a map:

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

var text = "whatever";
var url = map[text] === undefined ? 'http://www.mydomain./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./noone.html';
switch(text) {
  case 'amy': url = 'http://www.mydomain./amylikescats.html';
  break;
  case 'dave': url = 'http://www.mydomain./daveshome.html';
  break;
  case 'steve': url = 'http://www.mydomain./steve2.html';
  break;
  case 'jake': url = 'http://www.mydomain./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./noone.html';
break;

Associative array:

var data = {
  amy: 'http://www.mydomain./amylikescats.html',
  dave: 'http://www.mydomain./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./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./noone.html';
} else {
    url = 'http://www.mydomain./' + 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./amylikescats.html',
     'dave': 'http://www.mydomain./daveshome.html',
    'steve': 'http://www.mydomain./steve2.html',
     'jake': 'http://www.mydomain./jakeeatstofu.html'
};

var url = urlsForText[text] || 'http://www.mydomain./noone.html';
发布评论

评论列表(0)

  1. 暂无评论