Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript command to check values in set?

Tags:

javascript

Im trying to find a creative way/patch in order to minimize this line :

 if(myParam == '1' || myParam == 'a' || myParam == '*' || myParam == '@' || myParam == undefined || myParam == null || myParam == ' ')
    {...
    }

One solution is the use indexOf - but it is not cross browser (I could write my own func to behave like indexOf - but I dont want to ).

So I tried the in operator

But the in operator deals only with object properties names and indexes

So I tried this (object properties):

   if(window.lala in {
    '*': 0,
    'a': 0,
    '@': 0,
    ' ': 0,
    undefined: 0,
    null: 0
}) alert('1')

and I think it is working.

2 question please :

question #1

is it safe for property name to be [undefined] or [' '] or [null] ? will it always work ?

question #2 Is there any other solution for doing this ?

(Case/switch can be done too, I know... )

like image 254
Royi Namir Avatar asked Jun 21 '26 06:06

Royi Namir


2 Answers

What you have won't work for undefined. You can have a property named "undefined", but that's a string name, not the actual undefined value. You may get a match via type conversion, but it will also match if you have a string named "undefined" too which is not something you want.

I would suggest a more explicit test that uses === to avoid any type conversions. There are several ways to do it that are slightly less verbose than what you had:

if (myParam === null || myParam === undefined || 
    (myParam && myParam.length == 1 && "1a*@ ".indexOf(myParam) !== -1) {
   // code here
}

Or you can make an array and your own cross browser function (or use a polyfill for Array.indexOf() to search an array which can hold all the values:

function inArray(val, array) {
    for (var i = 0; i < array.length; i++) {
        if (array[i] === val) {
            return(true);
        }
    }
    return(false);
}

var testValues = [null, undefined, "1", "a", "*", "@", " "];
if (inArray(window.lala, array)) {
    // your code here
}

You can use the object form you were experimenting with and just use explicit tests for null and undefined, but I think the string.indexOf() suggestion in my first proposal is simpler. Anyway, here's a safe form using the object

if (myParam === null || myParam === undefined || 
    (myParam && (myParam in {"*":0, "1":0, "a":0, "@":0, " ":0})) {
   // code here
}

My recommendation would be the first option as I think it's the cleanest and it's also really easy to extend by just adding more chars to the test string.

like image 92
jfriend00 Avatar answered Jun 22 '26 21:06

jfriend00


Is it safe for property name to be [undefined] or [' '] or [null] ? will it always work?

All property names are converted to strings, which means you cannot distinguish between the number 0 and the string '0' and not between the value null (undefined) and the string 'null' ('undefined').

Treating numbers as numeric strings is probably fine in your case, for undefined and null, I would just add an extra test:

if(value == null || value in exclude) {
    // ...
}

value == null is true if value is null or undefined.

like image 35
Felix Kling Avatar answered Jun 22 '26 21:06

Felix Kling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!