skip to Main Content

I have an object with a format like this:

const obj = {
  key1: 'key1',
  key2: ''
}

I want to do specific actions based on whether the value of the keys (key1 and key2 above) are both present, both not present, or if one or the other key is present. I have the following code:

const getValues = (obj) => {
    if (!obj.key1 && !obj.key2) {
      //Do something when both keys aren't present
    }

    if (!obj.key1 && obj.key2) {
      //Do something when ONLY key2 is present
    }

    if (obj.key1 && !obj.key2) {
      //Do something when ONLY key1 is present
    }

    //Do something when both keys are present
}

const finalValue = getValues(obj)

Is there a way to simplify the above code without using multiple if else statements. I can’t use multiple variables in case statements so was wondering on alternatives.

5

Answers


  1. There’s nothing wrong with if/else, but if you want someting different, you might use ternary operator.

    Also, there’s a difference between key present and key has truthy value.
    In the snippet below I assume key present means key defined in the Object.

    const getValues = (obj) => {
      'key1' in obj && 'key2' in obj ?
        //Do something when both keys are present
        console.log(JSON.stringify(obj) + ' both keys are present')
    
      :'key1' in obj ?
        //Do something when ONLY key1 is present
        console.log(JSON.stringify(obj) + ' only key1 is present')
    
      :'key2' in obj ?
        //Do something when ONLY key2 is present
        console.log(JSON.stringify(obj) + ' only key2 is present')
    
      //Do something when both keys aren't present
      :console.log(JSON.stringify(obj) + ' no keys are present')
    }
    
    getValues({key1: false, key2: 2});
    getValues({key1: 0});
    getValues({key2: ''});
    getValues({});
    Login or Signup to reply.
  2. I know you said you don’t want multiple if/else. But You would actually want to use if/else if/else. This way the if sequence will stop and not keep processing each if statement separately like your code.

    There isn’t a lot to do to simplify your use case, just correct it.

    const getValues = (obj) => {
        if (!obj.key1 && !obj.key2) {
          //Do something when both keys aren't present
        }
        else if (!obj.key1 && obj.key2) {
          //Do something when ONLY key2 is present
        }
        else if (obj.key1 && !obj.key2) {
          //Do something when ONLY key1 is present
        }
        else{
            //Do something when both keys are present
        }
    }
    
    Login or Signup to reply.
  3. You could always nest conditions:

    if(obj.key1) {
        if(obj.key2) {
            // Both are true.
        } else {
        {
            // Only key1 is true.
        }
    } else
    {
        if(obj.key2) {
            // Only key2 is true.
        } else {
            // Neither is true.
        }
    }
    

    Another way might be to create a two-bit map and then run a switch statement on the bitmap.

    Login or Signup to reply.
  4. You can use ifs without elses

    const getValues = (obj) => {
        if (!obj.key1 && !obj.key2) {
          return "a";
        }
    
        if (!obj.key1 && obj.key2) {
          return "b";
        }
    
        if (obj.key1 && !obj.key2) {
          return "c";
        }
    
        return "d";
    }
    

    If you change the order, you can do something like this:

    const getValues = (obj) => {
        if (!obj.key1 && !obj.key2) {
          return "a";
        }
    
        if (obj.key1 && obj.key2) {
          return "d";
        }
    
        return obj.key1 ? "c" : "b"; 
    }
    
    Login or Signup to reply.
  5. Just for the fun of it:

    I don’t really recommend to use this, but it’s short and even shorter when it comes to 3 choices or more:
    code the choices in a binary number and make s switch:

    const getValues = (obj) => {
    
      switch (!('key1' in obj) * 2 + !('key2' in obj) ) {
       case 0b11 : return 'a';
       case 0b10 : return 'b';
       case 0b01 : return 'c';
       default : return 'd';
      }
    }
    console.log(getValues({key1: false, key2: 2}));
    console.log(getValues({key1: 0}));
    console.log(getValues({key2: ''}));
    console.log(getValues({}));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search