2

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?'

Cheeky Cherub
  • 123
  • 1
  • 2
  • 7
  • 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: http://stackoverflow.com/a/2573153/649979 – Graham Smith Aug 07 '12 at 10:53

5 Answers5

14

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.

Jon
  • 428,835
  • 81
  • 738
  • 806
5

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;
Aesthete
  • 18,622
  • 6
  • 36
  • 45
0

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];
}
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • In JavaScript, they’re called objects. – Paul D. Waite Aug 07 '12 at 10:58
  • @PaulD.Waite - pedantic much? [This goog search](https://www.google.co.uk/search?q=javascript+associative+array&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-GB:official&client=firefox-a) indicates that the term "associative array" is quite often linked with javascript (judging by the ~400K results). – Jamiec Aug 07 '12 at 11:23
  • True true, and the concept of an associative array (if I understand properly) perfectly applies to JavaScript objects. It’s just not what they’re called in JavaScript. – Paul D. Waite Aug 07 '12 at 11:51
0

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';
}
Esailija
  • 138,174
  • 23
  • 272
  • 326
0

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';
Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270