I need to findout if the JSON variable is undefined or not. Below is the snippet of code.
var file = JSON.parse(getvar("fileJSON"));
console.log(JSON.stringify(file));
// set required variables
setvar("a",file.a);
setvar("b",file.b);
setvar("c",file.c);
setvar("d",file.d);
setvar("e",file.e);
setvar("f",file.f);
Below check works fine. But instead of repeating for each variable, Can you please suggest common function in javascript to acheive this check "if(typeof file.a!== 'undefined')". Please suggest
if(typeof file.a!== 'undefined')
setvar("a",file.a);
if(typeof file.b!== 'undefined')
setvar("b",file.b);
Below function dint work :
setvar("a",checkNull(file.a));
function checkNull(var) {
if(typeof var !== 'undefined'){
return var;
}
}
Via Active questions tagged javascript - Stack Overflow https://ift.tt/XPUBq6k
Comments
Post a Comment