I have object with several properties, says it's something like this
{ a: "", b: undefined }
in jsx is there any one line solution I can check whether that object's property is not empty or has value or not? If array there's a isEmpty method.
I tried this
const somethingKeyIsnotEmpty = Object.keys((props.something, key, val) => {
return val[key] !== '' || val[key] !== undefined
})
Lodash _.has() Method The _.has() method is used to check whether the path is a direct property of object or not. It returns true if path exists, else it returns false.
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.
keys(object) method: The required object could be passed to the Object. keys(object) method which will return the keys in the object. The length property is used to the result to check the number of keys. If the length property returns 0 keys, it means that the object is empty.
But Sometimes You Do Need Lodash Not every Lodash utility is available in Vanilla JavaScript. You can't deep clone an object, for example. That's why these libraries are far from obsolete. But if you're loading the entire library just to use a couple of methods, that's not the best way to use the library.
In lodash, you can use _.some
_.some(props.something, _.isEmpty)
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