Files
30-seconds-of-code/snippets/formToObject.md
2019-03-15 12:54:57 +03:00

453 B

formToObject

Encode a set of form elements as a object.

First we transform the form into FormData, then we convert it into an array and from the array we collect an object

const formToObject = form =>
  Array.from(new FormData(form))
    .reduce((acc, [key, value]) => ({
      ...acc,
      [key]: value,
    }), {})
formToObject(document.querySelector('#form')) // { email: 'test@email.com', name: 'Test Name' }