Search code examples
javascriptsqllogicstatements

How to set the flag true/false based on field values?


I have multiple records that have type and status. In case where record type is 'DEV' then the status should be checked. In that case if status is 'Y' then variable should be set to true. Here is example of few records:

RecID  Type  Status
1      PROD    N
2      PROD    N 
3      PROD    N
4      DEV     Y
5      TEST    N

I have developed logic that works, here is example:

var showRec = false;

if (type === 'DEV') {
   if(status === 'Y') {
      showRec = true;
   }
}else{
   showRec = true;
}

Then I'm able to use showRec variable to show hide the elements. I'm wondering if logic above can be simplified? I couldn't find better way to approach this problem. Also I was wondering if this could be done in SQL with CASE WHEN and then simply use that column that will have Yes\No or true\false. If anyone have any suggestions please let me know.


Solution

  • You could use a ternary which checks the type and takes, if true the reuslt of the other check or true.

    showRec = type === 'DEV' ? status === 'Y' : true;