开发者

JavaScript statement shorthand needed to be expanded

开发者 https://www.devze.com 2023-03-28 16:21 出处:网络
I have this statement: pn = r.notFound == \"true\" ? \"Not currently assigned\" : \"Currently assigned to VPMO \" + r.currentAssignment;

I have this statement:

pn = r.notFound == "true" ? "Not currently assigned" : "Currently assigned to VPMO " + r.currentAssignment;

and I need to add a condition for "undefined" and then a value to it. It basically needs to read something like…

r.notFound == "undefined开发者_开发问答" then “Already assigned to this project”

else if r.notFound == “true” then “Not currently assigned”

else

“Currently assigned to VPMO “ + r.currentAssignment;


var pn;

if (r.notFound == 'undefined') { // be aware that this checks for STRING undefined
  pn = 'Already assigned to this project';
} else if (r.notFound == 'true') { // be aware that this checks for STRING true
  pn = 'Not currently assigned';
} else {
  pn = 'Currently assigned to VPMO ' + r.currentAssignment;
}

?

EDIT

If you want to check whether the variable is defined use:

if (typeof r.notFound === 'undefined') 


you can use multiple nested ternary operator:

r.notFound == "undefined" ? 
    "Already assigned to this project" :
    (r.notFound == "true" ? "Not currently assigned" : "Currently assigned to VPMO " + r.currentAssignment) 
0

精彩评论

暂无评论...
验证码 换一张
取 消