user5396759
user5396759

Reputation: 51

javascript - using if statement for multiple conditions vs switch case

I have a situation where I want to write something along the lines of this to check the page urls for a string.

url example 1 - http://something.com/else/apples
url example 2 - http://something.com/else/oranges
url example 3 - http://something.com/else/bananas
...
url example 10 - http://something.com/else/kiwis

pseudo code:

if(window.location.href.indexOf('apples')>-1)
{
    alert('apples');
}
if(window.location.href.indexOf('oranges')>-1)
{
    alert('oranges');
}
....

As you can see it looks rather ugly to have multiple if statements so I'm looking for some advice on how to achieve this using a single if statement or possible a switch case. I had an idea about an if statement with several 'OR' statements, but I'm not sure if that's the best way of going about with this.

Thank you

Upvotes: 1

Views: 892

Answers (6)

grzesiekmq
grzesiekmq

Reputation: 439

switch acts the same as if it is used to increase readability of code

$('#fruit').keyup(function() {

  var c = $(this).val();
  var fruits = ['apple', 'banana', 'pear', 'orange', 'raspberry', 'strawberry', 'wildberry', 'cranberry', 'blueberry', 'limone', 'grapefruit', 'kiwi', 'mango'];

  for (var i = 0; i < fruits.length; i++) {
    switch (c) {
      case fruits[i]:
        console.log(fruits[i]);
        break;
      default:
        '';
        break;
    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Enter a fruit: <input type="text" id="fruit">

Upvotes: 0

rishipuri
rishipuri

Reputation: 1528

Like this?

var fruits = ['apples', 'oranges', 'bananas'];

var link = window.location.href.split("/").pop();

if (fruits.includes(link)) {
   alert(link);
}

Upvotes: 1

Domino
Domino

Reputation: 6768

What you're trying to do is map specific values to specific outcomes. You can do this with an actual map. You can associate anything to values in a map, even functions. Then, use your favorite way to loop across the object. Here's a silly example:

var fruitMap = {
  'oranges': 'I don\'t like those.',
  'apples': 'Three a day keeps the doctor away.'
};

for (var fruit in fruitMap) {
  if (window.location.href.indexOf(fruit) > -1) {
    alert(fruitMap[fruit]);
  }
}

Upvotes: 0

Adjit
Adjit

Reputation: 10305

If you need to output specific information based on what the URL says, then I would suggest maybe using JSON to get information quickly. This will allow you to avoid having to use a loop or any conditional statements, and you can essentially get any information you want. That information can even contain an HTML page/element to be loaded in -

var pageInfo = {
  "apple": "some info about apples",
  "orange": "some other info about oranges",
  "banana": "more banana information"
}

var urlPage = window.location.href.split("/").pop();

var info = pageInfo[urlPage];

//to test -
console.log(pageInfo["apple"]);
console.log(pageInfo["orange"]);
console.log(pageInfo["banana"]);

Upvotes: 0

Chris
Chris

Reputation: 59511

Like this?

alert(window.location.href.split("/").pop());

This takes whatever comes after the last / and alerts that. Of course, this will alert anything. If there is a "whitelist" of things to alert, then @Purefan might have a better solution. This is just the quick-'n-easy solution.

Upvotes: 0

Purefan
Purefan

Reputation: 1506

Something like this?

let urls = ["url1.com", "url2.com"]

urls.forEach((url) => {
    if (window.location.href.indexOf(url) > -1) {
        alert("Do something")
    }
})

The idea is to use an array instead of many variables, and to loop through its items with forEach

Upvotes: 4

Related Questions