Prepare repository for merge

This commit is contained in:
Angelos Chalaris
2023-05-01 22:35:56 +03:00
parent fc4e61e6fa
commit b3ad01863a
578 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,28 @@
---
title: Form to object
type: snippet
tags: [browser,object]
cover: sail-away-2
dateModified: 2020-10-19T22:49:51+03:00
---
Encodes a set of form elements as an `object`.
- Use the `FormData` constructor to convert the HTML `form` to `FormData` and `Array.from()` to convert to an array.
- Collect the object from the array using `Array.prototype.reduce()`.
```js
const formToObject = form =>
Array.from(new FormData(form)).reduce(
(acc, [key, value]) => ({
...acc,
[key]: value
}),
{}
);
```
```js
formToObject(document.querySelector('#form'));
// { email: 'test@email.com', name: 'Test Name' }
```