user1321237
user1321237

Reputation:

Can I simplify multiple if - else statements?

I have the following code in javascript?

if ($(form).data('action') == "Edit") {
  xx
} else if ($(form).data('action') == "Create"){
  xy
} else if ($(form).data('action') == "Delete"){
  xz
}

Is it possible for me to simplify this and also have some default path?

Upvotes: 3

Views: 2615

Answers (8)

Yinghan
Yinghan

Reputation: 1

var Actions = {
    'Edit' : function () {},
    'Create' : function () {},
    'Delete' : function () {},
    'default': function () {}
},

action = $(form).data('action');
Actions[action]() || Actions[default]();

Upvotes: 0

Ateş Göral
Ateş Göral

Reputation: 140032

Using a switch block is the most conventional way.

You can also lookup functions to execute from an object (hashmap):

var actions = {
    Edit: function () {
        // xx
    },
    Create: function () {
        // xy
    },
    Delete: function () {
        // xz
    }
};

var action = actions[$(form).data("action")];

if (action) {
    action();
} else {
    // unknown/default action
}

Upvotes: 1

kinakuta
kinakuta

Reputation: 9037

What it sounds like you're describing is a switch/case, but I don't find switch case to be any better than multiple if/else structures. I prefer using object hashes:

var actionObj = {
  "Edit": xx,
  "Create": xy,
  "Delete": xz
};

if (actionObj[act]) {
  // do whatever with actionObj[act] you need to
} else {
  // do your default action
}

It works especially well when the values are actually functions, then you can just call them:

var actionObj = {
  "Edit": function () {},
  "Create": function () {},
  "Delete": function () {}
};

if (actionObj[act]) {
  actionObj[act]();
} else {
  // default action
}

Upvotes: 4

Jack
Jack

Reputation: 9538

You could also create an object that contains your actions:

var Actions = {
    'Edit' : function () {},
    'Create' : function () {},
    'Delete' : function () {}
};

var action = $(form).data('action');

if (Actions.hasOwnProperty(action)) {
    Actions[action]();
}

Upvotes: 10

swati
swati

Reputation: 1757

use switch case :

var action = $(form).data('action')
switch(action)
{
case 'Edit': xx;
break;
case 'Create': yy;
break;
case 'Delete': zz;
break;
default: caption ="default";
}

or you can use ternary operator ( true ? 1 : 0 )

Upvotes: 0

ini
ini

Reputation: 201

Try CASE from here: http://www.tutorialspoint.com/javascript/javascript_switch_case.htm

Example from them:

<script type="text/javascript">
<!--
var grade='A';
document.write("Entering switch block<br />");
switch (grade)
{
   case 'A': document.write("Good job<br />");
            break;
   case 'B': document.write("Pretty good<br />");
            break;
   case 'C': document.write("Passed<br />");
            break;
   case 'D': document.write("Not so good<br />");
            break;
   case 'F': document.write("Failed<br />");
            break;
  default:  document.write("Unknown grade<br />")
}
document.write("Exiting switch block");
//-->
</script>

Upvotes: -1

AurA
AurA

Reputation: 12363

Use Switch if you want to handle multiple if else

switch(n)
{
case 1:
  execute code block 1
  break;
case 2:
  execute code block 2
  break;
default:
  code to be executed if n is different from case 1 and 2
}

Upvotes: 3

Jack
Jack

Reputation: 9538

You should be caching the results of $(form).data('action'); Example:

var action = $(form).data('action');

if (action === 'Edit') {
    //
} else if (action === 'Create') {
    //
} else if (action === 'Delete') {
    //
}

Upvotes: 1

Related Questions