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
  • 28,982
  • 82
  • 233
  • 406

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,041
  • 8
  • 16
0

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

Undo
  • 25,381
  • 37
  • 106
  • 126
Tianxiang Zhang
  • 162
  • 1
  • 13