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
  • 30,952
  • 84
  • 245
  • 427

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,519
  • 37
  • 106
  • 129
Tianxiang Zhang
  • 162
  • 2
  • 13