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...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With