xxxxxxxxxx
function cleanObject(obj) {
// Base case for non-objects or null
if (obj === null || typeof obj !== 'object') {
return obj;
}
// For arrays, map through and clean each element
if (Array.isArray(obj)) {
return obj.map(value => cleanObject(value)).filter(value => value !== undefined);
}
// For objects, remove keys with undefined or null values
const cleanedObj = {};
for (const [key, value] of Object.entries(obj)) {
if (value !== undefined && value !== null) {
cleanedObj[key] = cleanObject(value); // Recurse into the value
}
}
return cleanedObj;
}