views:

41

answers:

4

Hi,

have this line:

 if($('#address_nations_id').val() != 105 && $('#address_nations_id').val() != 74)

Is there any way to make it shorter?

I tried this:

if($('#address_nations_id').val() != 105 && != 74)

But it doesnt work.

Any idea?

Regards

Javier

+4  A: 

Since I'm guessing (based on the ID) you want to check many more, you can use $.inArray() to expand that list easily, for example:

if($.inArray($('#address_nations_id').val(), ["105", "74"]) > -1)

You can give it a try here, keep in mind you'll need strings for this method, since JavaScript's weak typing with a == doesn't work here, and .val() returns a string.

Nick Craver
Witch does not increase readability ;) But it is better if you have to check a lot values.
Felix Kling
@nick +1 bit complex though, but thanks for new method of implementation.
Ninja Dude
@Avinash - Let's say you have 30 nations to check, and this is determining if they have a state drop down, etc...this gets much easier to maintain ;)
Nick Craver
I would use a object instead. Searching an array takes longer than looking up an object property.
Felix Kling
@Felix - Not internally it doesn't, the browser is still looping through an object based on the string to find a property, how did you come to this conclusion? Also, speed isn't really the main concern here, my previous comment is the most likely application, and this is *much* easier to maintain, and a bit smaller to boot.
Nick Craver
@nick yes, this is very use full then, more over `.val()` returns a string not a number, your implementation is pretty good
Ninja Dude
@Nick: Ok. I thought that it might be faster. Sorry. I was comparing with dictionaries and lists in Python...
Felix Kling
@Felix - It depends on the implementation and the browser...it is indeed hard to test because `object["string"]` gets optimized by the tracing engine to be a direct reference in most newer browsers...but the bigger issue is what's maintainable in this situation IMO, the speed is going to be negligible in any case (how many nations are there anyway?).
Nick Craver
@Nick: I like this one. It scales nicely and does not require extra variables. I would fix the weak typing issue by doing the following: `$.inArray(parseInt($('#address_nations_id').val(), 10), [105, 74])` This might be faster as well.
elusive
Btw I think it should be ` == -1` as the OP wants to do something if the value is **not** 105 or 74.
Felix Kling
+3  A: 

One thing you can do is to get the value beforehand:

var val = $('#address_nations_id').val();
if(val != 105 && val != 74)

Which is better anyway as you do only one method call instead of two.

If you have a lot of values, I would create a lookup table like so:

var values = {
    "105": 1,
    "74": 1
}

and do

if(!($('#address_nations_id').val() in values)) {
    //...
}

This should be faster than searching a value in an array. (not sure about this).

Felix Kling
Saves one *apparent* method call, in fact saving several under the covers.
T.J. Crowder
A: 
var val = $('#address_nations_id').val();

if(val != '105' && val != '74') { 
  //do stuff
}

Edit :

$('#address_nations_id').val() returns a string not a number

Ninja Dude
A: 

I'd go with Felix's suggestion. But just for completeness:

On browsers that have the indexOf on arrays, you could do:

if ([105, 74].indexOf(parseInt($('#address_nations_id').val())) >= 0)

...but I wouldn't recommend it for just two values. It's less clear, unnecessarily complex, and not all browsers have it (yet). Most of those criticisms apply to using jQuery's inArray function as well.

There's also switch:

switch (parseInt($('#address_nations_id').val())) {
    case 105:
    case 74:
        break;
    default:
        /* do your thing here *?
        break;
}

...but that hardly qualifies as shorter, almost certainly wouldn't be appropriate for just two values.

T.J. Crowder
Nick Craver
@Nick: The OP can determine whether a radix is required. The question gave two values, so that's what I commented on. If there are 30 values, it changes the question, and the answer.
T.J. Crowder
@TJCrowder - I have to disagree, you should *by default* provide a radix IMO, much safer :)
Nick Craver
@Nick: And if the OP agrees, he/she can decide to include one. If I got into commenting on everything I saw on SO that I thought was non-optimal... Let's just say the mods would have to get involved. ;-)
T.J. Crowder
@TJCrowder - I completely agree with non-optimal but that's not the case here to me with radix specifically. It's the very real potential of someone finding this and having "010" fail and have no idea why...or that the radix argument even exists, there are plenty of questions here to show that very many are unaware of it :)
Nick Craver