Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Iterate over an object and remove false property in JavaScript
In JavaScript, you may need to recursively iterate through an object and remove properties with falsy values. This is common when cleaning up configuration objects or filtering data structures.
The Problem
Consider an object with nested structures containing "enabled" properties. We want to remove any objects where enabled is false, and also clean up any empty parent objects left behind.
const obj = {
a: {
someKey: {
propOne: '',
enabled: true
}
},
b: {
someKey: {
propOne: '',
enabled: false
}
},
c: {
someKey: {
propOne: '',
enabled: false
}
},
someKey: {
ab: {
propOne: '',
enabled: true
}
}
};
console.log("Original object:");
console.log(JSON.stringify(obj, null, 2));
Original object:
{
"a": {
"someKey": {
"propOne": "",
"enabled": true
}
},
"b": {
"someKey": {
"propOne": "",
"enabled": false
}
},
"c": {
"someKey": {
"propOne": "",
"enabled": false
}
},
"someKey": {
"ab": {
"propOne": "",
"enabled": true
}
}
}
Solution: Recursive Property Removal
The solution uses recursion to traverse the object tree, checking each nested object for the enabled property and removing objects where it's false.
const obj = {
a: {
someKey: {
propOne: '',
enabled: true
}
},
b: {
someKey: {
propOne: '',
enabled: false
}
},
c: {
someKey: {
propOne: '',
enabled: false
}
},
someKey: {
ab: {
propOne: '',
enabled: true
}
}
};
const deleteFalsyKey = obj => {
const keys = Object.keys(obj);
keys.forEach(key => {
if(obj[key].enabled === false){
delete obj[key];
return;
};
if(obj[key] && typeof obj[key] === 'object'){
deleteFalsyKey(obj[key]);
if (!Object.keys(obj[key]).length) {
delete obj[key];
};
}
});
};
deleteFalsyKey(obj);
console.log("After removing falsy enabled properties:");
console.log(JSON.stringify(obj, null, 2));
After removing falsy enabled properties:
{
"a": {
"someKey": {
"propOne": "",
"enabled": true
}
},
"someKey": {
"ab": {
"propOne": "",
"enabled": true
}
}
}
How It Works
The function works in these steps:
-
Get all keys:
Object.keys(obj)returns an array of the object's property names -
Check for falsy enabled: If
obj[key].enabled === false, delete the entire object - Recurse into nested objects: For each property that is an object, call the function recursively
- Clean up empty objects: After recursion, delete any objects that have no remaining properties
Enhanced Version with Error Handling
Here's a more robust version that handles edge cases:
const deleteFalsyKeyRobust = (obj) => {
if (!obj || typeof obj !== 'object') {
return;
}
const keys = Object.keys(obj);
keys.forEach(key => {
const value = obj[key];
// Check if this object has an enabled property that is false
if (value && typeof value === 'object' && value.enabled === false) {
delete obj[key];
return;
}
// Recursively process nested objects
if (value && typeof value === 'object') {
deleteFalsyKeyRobust(value);
// Remove empty objects after recursion
if (Object.keys(value).length === 0) {
delete obj[key];
}
}
});
};
// Test with the same object
const testObj = {
a: { someKey: { propOne: '', enabled: true } },
b: { someKey: { propOne: '', enabled: false } },
c: { someKey: { propOne: '', enabled: false } }
};
deleteFalsyKeyRobust(testObj);
console.log("Result:", JSON.stringify(testObj, null, 2));
Result: {
"a": {
"someKey": {
"propOne": "",
"enabled": true
}
}
}
Conclusion
This recursive approach effectively removes objects with falsy enabled properties and cleans up empty parent objects. The key is using Object.keys() to iterate and recursion to handle nested structures, making it suitable for complex object hierarchies.
