0

I hope this makes sense. If I have an object:

 var a = {"minlength":true}
 var a = {}

How can I tell if the object is empty (the second line of code)

Samantha J T Star
  • 26,790
  • 72
  • 224
  • 390

3 Answers3

5
Object.keys(a).length === 0

should do the trick.

0

You should check for undefined as well:

if (a != undefined) {
    // object is defined, you can do stuff now
}
Code Whisperer
  • 1,016
  • 8
  • 15
0

Object.keys(a) returns a list of keys, so Object.keys(a).length == 0 means it's empty.

Undo
  • 25,204
  • 37
  • 102
  • 124
Tianxiang Zhang
  • 162
  • 1
  • 13