Skip to content

Instantly share code, notes, and snippets.

@BrunoQuaresma
Created August 25, 2020 14:48
Show Gist options
  • Select an option

  • Save BrunoQuaresma/3ee3bd37a6635bafe7b8e6577ddc16f9 to your computer and use it in GitHub Desktop.

Select an option

Save BrunoQuaresma/3ee3bd37a6635bafe7b8e6577ddc16f9 to your computer and use it in GitHub Desktop.

Revisions

  1. BrunoQuaresma created this gist Aug 25, 2020.
    18 changes: 18 additions & 0 deletions initializeForm.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    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
    }
    }, {})
    }