const obj = {
group: "rngs",
includeMedia: false,
orderBy: "publishedDate",
topicCodes: []
}
"", [], {}
const entries = Object.entries(obj);
const mustBeRemoved = v =>
(v instanceof Object && !Object.keys(v).length) ||
(!v && typeof v !== 'boolean');
const newObj = Object.fromEntries(entries.filter(n => !mustBeRemoved(n[1])));
entries.forEach(n => mustBeRemoved(n[1]) && delete obj[n[0]]);
function filterObject(target) {
return Object.keys(target).reduce((result, key) => {
const value = target[key];
if (!value && typeof value !== "boolean") {
return result;
}
if (typeof value === "object" && Object.keys(value).length === 0) {
return result;
}
return {
...result,
[key]: value
};
}, {});
}
const filterObj = Object.keys(obj).filter(key =>
obj[key].length > 0 || typeof obj[key] === "boolean"
)
console.log('filterObj :', filterObj)