Sunday, January 18, 2026
HomeLanguagesJavascriptRemove blank attributes from a JavaScript Object

Remove blank attributes from a JavaScript Object

Given a JavaScript Object with null values in it, the task is to remove those null values from the object using JavaScript. Below are the methods to remove the blank attributes:

JavaScript delete property: This keyword deletes a property of an object. It deletes both the value and the property also. After deletion, the property is not available for use. This operator can only be used on object properties. It can not be used on variables, functions, or predefined JavaScript object properties. 

Syntax:

delete Object.property or
delete Object['propertyName']

Example 1: This example goes through each object’s key and checks if the property has a null value. If it has then it deletes that property by using Delete property.

Javascript




let obj = {
    prop_1: null,
    prop_2: 'GFG',
    prop_3: 3,
    prop_4: null
}
  
Object.keys(obj).forEach(
    (key) => (obj[key] === null) && delete obj[key]);
  
console.log(JSON.stringify(obj));


Output

{"prop_2":"GFG","prop_3":3}

Example 2: This example goes through each key of the object and check if property have null and undefined values as well. If it has it deletes that property by Delete property.

Javascript




let obj = {
    prop_1: null,
    prop_2: 'GFG',
    prop_3: 3,
    prop_4: null
}
  
function delet(obj) {
    for (let prop in obj) {
        if (obj[prop] === null || obj[prop] === undefined) {
            delete obj[prop];
        }
    }
}
  
delet(obj);
  
console.log(JSON.stringify(obj));


Output

{"prop_2":"GFG","prop_3":3}
RELATED ARTICLES

Most Popular

Dominic
32474 POSTS0 COMMENTS
Milvus
118 POSTS0 COMMENTS
Nango Kala
6846 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12063 POSTS0 COMMENTS
Shaida Kate Naidoo
6985 POSTS0 COMMENTS
Ted Musemwa
7219 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6911 POSTS0 COMMENTS