I want to return all elements in an object that has no value, empty or null.. (i.e)
{
firstname: "John"
middlename: null
lastname: "Mayer"
age: ""
gender: "Male"
}
I want to return the middlename and the age in the oject. Please help me. Thank you.
Lets say that your request object is in a variable called obj. You can then do this:
Object.keys(obj).filter(key => obj[key] === null || obj[key] === undefined || obj[key] === "")
Object.keys will fetch all the keys of the object. You will then run a filter function over the object keys to find the required items.
Right now there are three conditions of null, undefined and empty string. You can add more as per your need.
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