Remove Empty String From An Object In JavaScript

In this article, we will explore a JavaScript technique to remove empty string from an object. We’ll walk through the steps involved in achieving this, including iterating over the object’s values, checking for empty strings, and utilizing the delete operator.

By the end of this article, you’ll have a clear understanding of how to implement this solution in your JavaScript code.

Remove Empty String From An Object In JavaScript

To begin, let’s consider a scenario where we have an object with various properties and values:

const obj = {

  key1: "value1",

  key2: "",

  key3: "value3",

  key4: null,

  key5: "value5",

  key6: undefined

};

Our goal is to remove any properties from this object that have an empty string as their value. We can accomplish this by iterating over the object’s keys and checking the corresponding values. If a value is an empty string, we’ll delete the property using the delete operator.

To iterate over the object’s keys, we have a couple of options. One approach is to use a for..in loop, which allows us to loop through all the enumerable properties of an object. In this case, we’ll choose the for..in loop because it reduces the number of lines of code needed.

Here’s how we can implement the removal of empty strings using a for..in loop:

for (const key in obj) {
  if (obj[key] === "") {
    delete obj[key];
  }
}

In the code snippet above, we loop through each key in the obj Object. If the corresponding value is an empty string (""), we use the delete operator to remove the property from the object.

However, it’s worth noting that in addition to empty strings, we may also want to remove undefined and null values from the object. This step is optional and depends on your specific requirements.

If you decide to remove these values as well, we can enhance our solution by encapsulating the condition within a function called checkInvalidValue. This function takes a value as an argument and checks if it is null, undefined, or an empty string. It returns true if the condition matches, indicating that the value should be removed.

Remove Empty And Invalid Values

Let’s define the checkInvalidValue function:

const checkInvalidValue = (value) => {
  if (value === null || value === undefined || value === "") {
    return true;
  } else {
    return false;
  }
};

Now, we can modify our for..in loop to utilize the checkInvalidValue function and remove the properties accordingly:

for (const key in obj) {
  if (checkInvalidValue(obj[key])) {
    delete obj[key];
  }
}

By incorporating the checkInvalidValue function, we ensure that properties with empty strings, null, and undefined values are all removed from the object.

To see the result of our code, we can log the modified object to the console:

remove empty string from an object in JavaScript

When executing the code, you will notice that the properties key2, key4, and key6 have been successfully removed from the object.

Conclusion

In conclusion, by leveraging the power of JavaScript and utilizing concepts such as iteration, conditionals, and the delete operator, we can effectively remove empty strings, null, and undefined values from an object. This technique allows us to create cleaner and more streamlined data structures in our JavaScript applications.

Leave a Reply