0

I'm trying to evaluate whether an object has a certain property like the following, as if I go directly to the last 'if', I might get undefined due to property1 or property2 being non-existent:

if (obj[property1]) {
  if (obj[property1][property2]) {
    if (obj[property1][property2][property3]) {
      console.log('property exists!')
    }
  }
}

I'm working with numbers. This will work if the VALUE of property3 is not 0, since 0 evaluates to falsy.

{
  property1: {
    property2: {
      property3: 0 // evaluates to false as 0 is falsy
    }
  }
}

How can I check if a object property exists regardless of its value?

uber
  • 4,163
  • 5
  • 26
  • 55
  • Consider using the "elvis operator" (optional chaining) `obj?.property1?.property2?.property3`? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining – evolutionxbox Feb 22 '21 at 17:35
  • Check for the presence of the key, not the value, if you don't care about the value. – Dave Newton Feb 22 '21 at 17:37
  • @evolutionxbox That's just shorthand for what they're doing now. – Dave Newton Feb 22 '21 at 17:38
  • 1
    `if( obj?.property1?.property2?.hasOwnProperty("property3") )` – adiga Feb 22 '21 at 17:38
  • @DaveNewton easiest way to do it in this 'nested object' case I presented? – uber Feb 22 '21 at 17:41
  • 1
    There is already a solution to this on SO: https://stackoverflow.com/questions/2631001/test-for-existence-of-nested-javascript-object-key – dLcreations Feb 22 '21 at 17:42
  • 1
    Does this answer your question? [Test for existence of nested JavaScript object key](https://stackoverflow.com/questions/2631001/test-for-existence-of-nested-javascript-object-key) – evolutionxbox Feb 22 '21 at 17:43

3 Answers3

2

You could use hasOwnProperty with optional chaining:

if( obj?.property1?.property2?.hasOwnProperty("property3") ){

}
adiga
  • 34,372
  • 9
  • 61
  • 83
0

You can use optional chaining.

return Number.isInteger(obj?.property1?.property2?.property3)

and in case property3 can be something else then number you can use Nullish Coalescing:

return obj?.property1?.property2?.property3 ?? false

Nullish Coalescing is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined.

You can read more about it here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator

Ran Turner
  • 14,906
  • 5
  • 47
  • 53
0

Rather than get complicated with nested or otherwise elaborate conditionals, simply wrap the access of the property in question with try/catch wrapper. It's very clean and simple, plus can handle any kind of object nesting complexity you might have. \

You can refine try/catch approach by determining the actual exception thrown on the property not being found to make it more robust. Otherwise you may have other exceptions create the illusion that the property does not exist and as a result mask other code issues.

Tim Holt
  • 3,044
  • 1
  • 18
  • 22