Aaron
Aaron

Reputation: 2500

Is there a better way to write this set of IF statements?

There are a couple spots in my code where I need to test for true on multiple different variables - like so:

if(setting1){ Do something with El A };
if(setting2){ Do something with El B };
if(setting3){ Do something with El C };
if(setting4){ Do something with El D };

Is there a better way of writing this?

I've updated the question with extra details, but it looks like the block of IFs is the best option.

Settings are saved to .data. These settings are true/false and they enable/disable HTML elements. At certain points in my script, I need to check these elements to see if they're enabled before running some additional code. For example, showing the elements - if the user has set the .data for the Nav Button to true - then the Nav button will be shown.

if(setting1){ Do A to El A };

There isn't really a pattern to the code inside the condition, so I don't think a loop is a good solution. Again, I think a block of IFs is the best way to go, but I'm curious if there's some kind of cool trick to write this cleaner.

if(setting1){ Do A to El A};
if(setting2){ Do B to El B};
if(setting3){ Do A to El A};
if(setting4){ Do X to El X};

Thanks!

Upvotes: 0

Views: 105

Answers (4)

Web_Designer
Web_Designer

Reputation: 74660

Use Array.forEach to loop through each element checking if it's disabled:

elements.forEach(function(el) {
    alert(el.disabled);
});

Upvotes: 1

mgibsonbr
mgibsonbr

Reputation: 22017

If settingsX are arbitrary, Do something too, and there's no relation whatsoever between a setting and an element, a setting and the code or the element and the code, then I believe your code is minimal. I could suggest many alternative ways of expressing them - which may help promote reuse, but not make the code smaller - but I'd need more info about the context.

For instance, if both the code and the settings is specific to the element:

$("#elementA").data("DoSomething", function() { ... });
$("#elementB").data("DoSomething", function() { ... });

$(".my_elements")
    .filter(function() { return specificSettings(this); })
    .each(function() { $(this).data("DoSomething")(); });

Note that the resulting code is more verbose, so it's only an advantage if you're reusing it in several places.

Upvotes: 0

Ric
Ric

Reputation: 8795

If you're not one for KISS,

var settingsAndEffects = 
[
    {'test': function(params) { return bool; }, // setting1
        'apply': function(params) { ... }}, // Do something with El A
    {'test': function(params) { return bool; }, // setting2
        'apply': function(params) { ... }}, // Do something with El B
    ...
]

for(var i = 0; i < settingsAndEffects.length; i++) 
{
    if(settingsAndEffects[i].test(params))
        settingsAndEffects[i].apply(params);
}

otherwise stick with the ifs.

Upvotes: 1

Kevin Carmody
Kevin Carmody

Reputation: 2319

Use a switch statement ... [filler text here]

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/switch

Upvotes: 0

Related Questions