admin管理员组

文章数量:1410674

If I need to check an attribute of an object, and it's object (and so on) but can't be sure whether the object exists in the first place how can I make the condition simpler than this?

if (a !== undefined && a.b !== undefined && a.b.c === 'foo')

I'm also using LoDash if it has any meaningful function.

If I need to check an attribute of an object, and it's object (and so on) but can't be sure whether the object exists in the first place how can I make the condition simpler than this?

if (a !== undefined && a.b !== undefined && a.b.c === 'foo')

I'm also using LoDash if it has any meaningful function.

Share Improve this question edited May 22, 2017 at 19:08 user1211 1,5151 gold badge18 silver badges27 bronze badges asked May 22, 2017 at 19:05 HedgeHedge 16.8k45 gold badges154 silver badges261 bronze badges 1
  • 1 That code will throw an error when a doesn't exist. See my answer below for correct code. – Scott Marcus Commented May 22, 2017 at 19:41
Add a ment  | 

4 Answers 4

Reset to default 5

if you know that a is in the current scope (like ming in as an argument) you can do
if(a && a.b && a.b.c === 'foo')
but if ther's an chance a is never defined you have to check the type like:
if( typeof a != 'undefined' && a.b && a.b.c === 'foo')

update
when using lodah the has function can give an better syntax (that unlike the get function returns true if the propperty is a falsy value. Thanks @ryeballar): if(lodash.has(a,'b.c')){ console.log(a.b.c); }

Lodash has _.get which allows you to define a keypath towards a nested item in the object. It handles the intermediate checks for you and simply returns either undefined or the default value if the key is not found.

if(_.get(a, 'b.c') === 'foo')

If a truly does not exist, you are going to want a try/catch, which means you could do:

try {
    if (a.b.c === 'foo') {
        // Do stuff.
    }
} catch (ignore) {}

Before you worry about making your code shorter, you need to know that your code will throw an error when a doesn't exist:

if (a !== undefined && a.b !== undefined && a.b.c === 'foo'){
  console.log("a exists, a.b exists and a.b.c === 'foo'");
}

You cannot access a non-existent object without getting an error. So, you need to check the type of a to see if it is not "undefined" (note, the value to check against is a string).

function checkObj(){
  if (typeof a !== "undefined" && a.b && a.b.c === 'foo'){
    console.log("a exists, a has a b property and a.b.c === 'foo'");
  } else {
    console.log("Not all conditions met.");
  }
}

checkObj();  // a doesn't exist --> "Not all conditions met."

var a = {};

checkObj(); // a exists, but b and c don't --> "Not all conditions met."

a.b = {};

checkObj();  // a and b exist but c doesn't --> "Not all conditions met."

a.b.c = "test";

checkObj();  // a, b and c exist, but c has wrong value --> "Not all conditions met."

a.b.c = "foo";

checkObj();  // "a exists, a has a b property and a.b.c === 'foo'"

本文标签: javascriptShorter way to do if (aundefined ampamp abundefined ampamp abc39foo39)Stack Overflow