Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check for empty property value using lodash

Tags:

reactjs

lodash

I am trying to check the following for empty values using lodash:

data.payload{
      name: ""
}

My code:

import isEmpty from 'lodash.isempty';

if (isEmpty(data.payload)) {

The above is false, how can I validate for empty values?

I have this code in my helper:

export const isEmpty = some(obj, function(value) {
  return value === '';
}

);

In my action I have

    if (isEmpty(data.payload)) {

I get an error, ReferenceError: obj is not defined but I am passing the object...

like image 601
Bomber Avatar asked Mar 08 '23 09:03

Bomber


1 Answers

If you want to use isEmpty() to check for objects with nothing but falsey values, you can compose a function that uses values() and compact() to build an array of values from the object:

const isEmptyObject = _.flow(_.values, _.compact, _.isEmpty);

isEmptyObject({});
// -> true

isEmptyObject({ name: '' });
// -> true

isEmptyObject({ name: 0 });
// -> true

isEmptyObject({ name: '...' });
// -> false
like image 137
Adam Boduch Avatar answered Mar 19 '23 09:03

Adam Boduch