How To Remove Empty String Values From An Object Using JavaScript

Remove Empty String Values from an Object using JavaScript

Are you looking for ways to remove empty string values from an object using Javascript? In this article, you will learn two ways to do that. Let’s read this article now.

Remove empty string values from an object using JavaScript

Using object.keys() property and forEach() method

The idea for removing empty string values from an object is that we will get an array containing all the object’s keys using the Object.keys() property. Then iterate through the array using the forEach() method. Finally, we will check whether the corresponding value is empty for each key. If it is an empty value, we use the keyword delete to delete that key in the object.

Code sample

let obj1 = {
    name: 'Marry',
    address:'',
    age: 20,
    hobby:''
}

//check each value against the array of keys 
Object.keys(obj1).forEach(key => {
    if(obj1[key] == '')
    delete obj1[key];
});
 
console.log(obj1);

Output

{name: 'Marry', age: 20}

The object.keys property is a method used to create an array with all an object’s keys.

The delete operator can completely remove properties from a JavaScript object. This is the only way to remove a property from an object. Setting a property to undefined or null simply changes the value of that property. It does not completely remove the property from the object.

Code sample

let obj1 = {
    name: 'Marry',
    address:'',
    age: 20,
    hobby:''
}

//check each value against the array of keys 
Object.keys(obj1).forEach(key => {
    if(obj1[key] == '')
    delete obj1[key];
});
 
console.log(obj1.address);

Output

undefined

Using for…in loop

The idea is the same as the one above, and we can use the for…in loop to remove empty string values from an object using Javascript.

Syntax:

for (variable in object) {
   // statements
} 

Parameters:

  • variable: The name of a variable, an element of an array, or a property of an object.
  • object: An object or the name of a valid object whose properties will be repeated.
  • statements: Statements to execute each iteration.

Why use the for…in loop?

In fact, the for…in loop in JavaScript is used for debugging purposes, which is an easy way to check the properties of an object (by printing it out in the console log).

Code sample

let obj2 = {
    people: '',
    color:'blue',
    number:'32',
    address:''
}

//directly check each value against the key in the obj2 object 
for(key in obj2){
    if(obj2[key]=='')
    delete obj2[key];
}
 
console.log(obj2);

Output

{color: 'blue', number: '32'}

Summary

Above, I showed you how to remove empty string values from an object using Javascript. I hope they are helpful for you. To better understand the lesson’s content, practice rewriting today’s examples. And let’s learn more about Javascript in the next lessons here. Have a great day!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *