lodash: check object is empty

Meldum picture Meldum · May 30, 2016 · Viewed 82.5k times · Source

I have this object: {"": undefined}

and when I check this object for empty in this way: _.isEmpty({"": undefined})

I get false result, maybe in lodash we have another method?

Answer

Armen Zakaryan picture Armen Zakaryan · Nov 28, 2017
_.isEmpty(obj, true)

var obj = {
  'firstName': undefined
, 'lastName' : undefined
};

console.log(_.isEmpty(obj)); // false
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

Please, see http://www.ericfeminella.com/blog/2012/08/18/determining-if-an-object-is-empty-with-underscore-lo-dash/