Last active
September 19, 2018 12:11
-
-
Save michalklim/08be31e04deeea1960f1a461acac7a28 to your computer and use it in GitHub Desktop.
Compact form data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const compactFormData = obj => { | |
const objectFilteredKeys = Object.keys(obj).filter(k => !isNil(obj[k]) && !(isString(obj[k]) && isEmpty(obj[k]))) | |
const assignData = (newObj, k) => | |
isObject(obj[k]) ? Object.assign(newObj, { [k]: compactFormData(obj[k]) }) : Object.assign(newObj, { [k]: obj[k] }) | |
if (isArray(obj)) { | |
return objectFilteredKeys.reduce(assignData, []) | |
} else if (isObject(obj)) { | |
return objectFilteredKeys.reduce(assignData, {}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment