const defaultIsMatchFn = val => val === null || val === undefined || val === '' export function clearObject(obj, isMatchFn = defaultIsMatchFn) { if (isMatchFn(obj)) return null if (typeof obj === 'object') { const initial = obj instanceof Array ? [] : {} const result = Object.keys(obj).reduce((acc, key) => { const value = clearObject(obj[key]) return Object.assign(initial, acc, defaultIsMatchFn(value) ? {} : { [key]: value }) }, initial) return Object.keys(result).length ? result : null } return obj }