object
.FormData
constructor to convert the HTML form
to FormData
and Array.from()
to convert to an array.Array.prototype.reduce()
.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' }
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️