Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lodash: check object is empty

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?

like image 794
Meldum Avatar asked May 30 '16 10:05

Meldum


People also ask

How do I check if a Lodash object is empty?

The Lodash _. isEmpty() Method Checks if the value is an empty object, collection, map, or set. Objects are considered empty if they have no own enumerable string keyed properties. Collections are considered empty if they have a 0 length.

What is _ isEmpty?

isEmpty() function: It is used to check whether a list, array, string, object etc is empty or not.

Is Empty object Falsy?

There are only seven values that are falsy in JavaScript, and empty objects are not one of them.


2 Answers

_.isEmpty(obj, true) 

var obj = {   'firstName': undefined , 'lastName' : undefined };  console.log(_.isEmpty(obj)); // false console.log(_.isEmpty({})); // true
<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/

like image 62
Armen Zakaryan Avatar answered Oct 14 '22 00:10

Armen Zakaryan


Your example object is not empty so instead perhaps you want to test if all properties are undefined

let o = {foo: undefined}; !_.values(o).some(x => x !== undefined); // true 
like image 36
Paul S. Avatar answered Oct 13 '22 23:10

Paul S.