Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if an object has empty properties [duplicate]

Let's say I have the following JS Object

data = {
    name: "John",
    dataOfBirth: "",
    externalId: 2548,
    email: "[email protected]",
    mobile: ""
}

I will receive an object like this with many more properties that can be either String, Integer or undefined. For me to update my database, I cannot override a valid information with an empty one.

I could try if (data.email === "") delete data.email with all of them, but that seems unpractical to maintain.

Is there any way I can scan all properties without having to name every single one of them and remove all empty/undefined ones?

like image 519
Caio Favero Avatar asked Aug 03 '18 19:08

Caio Favero


People also ask

How can you tell if an object has an empty property?

Using Object. Object. keys will return an Array, which contains the property names of the object. If the length of the array is 0 , then we know that the object is empty. We can also check this using Object.

How do you check if an object has a blank key value?

keys method to check for an empty object. const empty = {}; Object. keys(empty). length === 0 && empty.

Is Empty object Falsy value?

There are only seven values that are falsy in JavaScript, and empty objects are not one of them. An empty object is an object that has no properties of its own. You can use the Object.

How do you check if all values in an object are null?

allNull() is a static method of the ObjectUtils class that is used to check if all the values in the array point to a null reference. The method returns false if any value in the array of values is not null . The method returns true if all the values in the passed array are null or the array is null or empty.


2 Answers

You could simply loop through the object keys and check for each element if they value is blank.

var data = {
        name: "John",
        dataOfBirth: "",
        externalId: 2548,
        email: "[email protected]",
        mobile: ""
    }

    for(var key in data) {
        if(data[key] === "") {
           console.log(key + " is blank. Deleting it");
           delete data[key]
        }
    }
like image 78
basic Avatar answered Oct 04 '22 12:10

basic


Can't you do this? Or am I missing something?

Object.entries(data).filter(([k,v],i)=>!!v)

give you this:

"[
    [
        "name",
        "John"
    ],
    [
        "externalId",
        2548
    ],
    [
        "email",
        "[email protected]"
    ]
]"

!! will turn a value into boolean, at that stage you filter out null,NaN and undefined. Indeed, if you want to try this on nested objects, then you have to recursively do this because !!Object() is always true. Even better would be to recursively and asynchronously copy the object, filter out falsey primitives and pass a callback to stringify in the end.

Edit:

Someone below mentioned some falsey values that you might want to keep such as 0. In that case chain them:

v===null || v===0 || !!v //null, 0 and anything not falsey, but not undefined etc.
like image 43
ibrahim tanyalcin Avatar answered Oct 04 '22 14:10

ibrahim tanyalcin