Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
remove all empty value and empty object from object(for nested objects).
/**
* Recurcively remove empty value, empty object.
* you can add more checks on line 12 as per your requirments
**/
function removeEmpty(data) {
return Object.keys(data).reduce((accumulator, key) => {
const isObject = typeof data[key] === 'object';
const isDateTime = key === 'from_date_time' || key === 'to_date_time';
const value = isObject ? this.removeEmpty(data[key]) : data[key];
const isEmptyObject = isObject && !Object.keys(value).length;
if (value === '' || isEmptyObject) {
return accumulator;
}
return Object.assign(accumulator, {[key]: value});
}, {});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment