Update formToObject.md

This commit is contained in:
Angelos Chalaris
2019-04-08 08:36:00 +03:00
committed by GitHub
parent 2b84b1478b
commit b1d9b8620d

View File

@ -1,10 +1,9 @@
### formToObject
Encode a set of form elements as a `object`.
Encode a set of form elements as an `object`.
1. Convert the HTML form to `FormData()`
2. Convert `FormData()` to `Array` using ` Array.from()`
3. We collect an object from an array using `Array.prototype.reduce()`
Use the `FormData` constructor to convert the HTML `form` to `FormData`, `Array.from()` to convert to an array.
Collect the object from the array, using `Array.prototype.reduce()`.
```js
const formToObject = form =>
@ -17,4 +16,4 @@ const formToObject = form =>
```js
formToObject(document.querySelector('#form')) // { email: 'test@email.com', name: 'Test Name' }
```
```