42

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?

Meldum
  • 579
  • 1
  • 4
  • 6

8 Answers8

57
_.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/

danday74
  • 52,471
  • 49
  • 232
  • 283
Armen Zakaryan
  • 1,037
  • 1
  • 9
  • 7
  • Hi @Deano. What is the problem. – Armen Zakaryan Dec 01 '17 at 15:36
  • 63
    -1 for lack of clarity. Future readers: Note that isEmpty does **not** take any second argument. Consulting lodash's docs for this would only leave your more stumped. This functionality only comes when you write your own **mixin** and apply that to isEmpty. This answer should really specify how that happens. Read the linked blog post for more details. – kumarharsh Apr 23 '18 at 08:27
26

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
Paul S.
  • 64,864
  • 9
  • 122
  • 138
9

It depends on how you want to check it. Do you want to check some or every

Then what you can do is :

import { some, isEmpty } from 'lodash'
console.log(some(this.yourObject, isEmpty))
pkerckhove
  • 763
  • 2
  • 10
  • 17
5

In your case it cannot be called an empty object (Object.values(obj).length would return 1), but for a completely empty object this can be used:

import { matches } from 'lodash';
matches(obj, {});
truefusion
  • 485
  • 4
  • 9
2

I guess this is a bit overkill, but this is what I use which recursively checks for nested objects too and uses lodash.

function checkEmptyObject(obj) {
  if (_.isEmpty(obj)) return true;
  return _.isEmpty(
    Object.entries(obj)
      .map(([key, value]) => {
        if (_.isEmpty(value)) return true;
        if (value instanceof Object) return checkEmptyObject(value);
        return false;
      })
      .filter((b) => b === false)
  );
}
soulphoenix
  • 106
  • 7
2

I'd do it this way

_.isEmpty(_.omitBy(object_to_check, _.isNil)

_.omitBy removes all the keys with null & undefined values in this example. To remove only undefined you can use _.isUndefined instead of _.isNil

Zephyr
  • 1,612
  • 2
  • 13
  • 37
0

To provide an alternative to Lodash - this is how I do the same with vanilla JS ES6.

const isEmpty = (obj) => {
    return obj === undefined ? true : Object.keys(obj).length === 0
}

console.log(isEmpty(undefined))
console.log(isEmpty({}))
Hexodus
  • 12,361
  • 6
  • 53
  • 72
0

try this one

_.omitBy(
            obj,
            (v) =>
                _.isUndefined(v) ||
                _.isNull(v) ||
                v === '' ||
                (v && v instanceof Object && Object.keys(v).length === 0)
        );
Amr Omar
  • 399
  • 5
  • 12