-4

Imagine this scenario:

var myObject = {
    "1030":{},
    "1059":{}
}

I want to check if 1030 is in that object. How would I do this?

Nick
  • 9,113
  • 4
  • 25
  • 29
Amazing Aaron
  • 53
  • 1
  • 1
  • 4

1 Answers1

0

Try hasOwnProperty

if(myObject.hasOwnProperty("1030")) {
    // Do code
}

It's a bit safer than checking if(myObject["1030"]). This will return false, if the value is falsey (false, undefined, null), which may be desirable, but also does not strictly mean it does not exist.

Hopeful Llama
  • 728
  • 5
  • 26