30-seconds-of-code: Simplify formToObject

Created on 29 Jun 2019  路  3Comments  路  Source: 30-seconds/30-seconds-of-code

Description

formToObject could be simplified to use of two new methods:

FormData.prototype.entries()
Object.fromEntries()

They are supported in Chrome 73+, Firefox 63+ and Safari 12.1+

Previous version:

const formToObject = form =>
  Array.from(new FormData(form)).reduce(
    (acc, [key, value]) => ({
      ...acc,
      [key]: value
    }),
    {}
  );

Updated version:

const formToObject = form => Object.fromEntries(new FormData(form).entries())
enhancement on hold opinions needed

Most helpful comment

While this is an interesting option indeed, Object.fromEntries() worries me for the time being, due to it being a Draft still. I will put this on hold and we will revisit it in a few months, when this has moved forward a little bit.

All 3 comments

While this is an interesting option indeed, Object.fromEntries() worries me for the time being, due to it being a Draft still. I will put this on hold and we will revisit it in a few months, when this has moved forward a little bit.

Closing due to inactivity. Will reopen when these methods are in a better place so to speak.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for any follow-up tasks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fplgusmao picture fplgusmao  路  4Comments

Priyansh2001here picture Priyansh2001here  路  5Comments

Chalarangelo picture Chalarangelo  路  5Comments

Lucien-X picture Lucien-X  路  5Comments

skatcat31 picture skatcat31  路  5Comments