1. object-to-formdata
> Convenient JavaScript function that serializes Objects to FormData instances.
object-to-formdata
Package: object-to-formdata
Created by: therealparmesh
Last modified: Fri, 12 May 2023 14:02:38 GMT
Version: 4.5.1
License: MIT
Downloads: 373,295
Repository: https://github.com/therealparmesh/object-to-formdata

Install

npm install object-to-formdata
yarn add object-to-formdata

object-to-formdata

Convenient JavaScript function that serializes Objects to FormData instances.

npm
npm

Install

 npm install object-to-formdata

Usage

NOTE: STARTING WITH VERSION 4.0.0, THE NAMED EXPORT HAS CHANGED!

NOTE: STARTING WITH VERSION 3.0.0, THERE IS NO DEFAULT EXPORT!

 import { serialize } from 'object-to-formdata';

const object = {
  /**
   * key-value mapping
   * values can be primitives or objects
   */
};

const options = {
  /**
   * include array indices in FormData keys
   * defaults to false
   */
  indices: false,

  /**
   * treat null values like undefined values and ignore them
   * defaults to false
   */
  nullsAsUndefineds: false,

  /**
   * convert true or false to 1 or 0 respectively
   * defaults to false
   */
  booleansAsIntegers: false,

  /**
   * store arrays even if they're empty
   * defaults to false
   */
  allowEmptyArrays: false,

  /**
   * don't include array notation in FormData keys for any Attributes excepted Files in arrays
   * defaults to false
   */
  noAttributesWithArrayNotation: false,

  /**
   * don't include array notation in FormData keys for Files in arrays
   * defaults to false
   */
  noFilesWithArrayNotation: false,

  /**
   * use dots instead of brackets for object notation in FormData keys
   * defaults to false
   */
  dotsForObjectNotation: false,
};

const formData = serialize(
  object,
  options, // optional
  existingFormData, // optional
  keyPrefix, // optional
);

console.log(formData);

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code