Skip to content

Instantly share code, notes, and snippets.

@BrunoQuaresma
Created August 25, 2020 14:48
Show Gist options
  • Save BrunoQuaresma/3ee3bd37a6635bafe7b8e6577ddc16f9 to your computer and use it in GitHub Desktop.
Save BrunoQuaresma/3ee3bd37a6635bafe7b8e6577ddc16f9 to your computer and use it in GitHub Desktop.
Form Initialize for React
function initializeForm(form) {
return Object.keys(form).reduce((newForm, key) => {
let value
if(form[key] === null || form[key] === undefined) {
value = ''
} else if(typeof form[key] === 'string') {
value = form[key]
} else if(typeof form[key] === 'object') {
value = initializeForm(form[key])
}
return {
...newForm,
[key]: value
}
}, {})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment