Skip to content

A standalone js function to convert an object to a query string

Notifications You must be signed in to change notification settings

dwilt/obj-to-query-string

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This dependency-free function will take an object and convert it to a query string.

Installation

In the root of your project, run:

yarn add obj-to-query-string

Usage

The function takes a single parameter, an object. The object must contain only strings, numbers, or an array of numbers/strings as it's properties as I can't think of a sensible way to do deep nesting for objects.

Any null or undefined values on the object or in any arrays on the object will be ignored.

import toQueryString from "obj-to-query-string";

const myObject = {
  a: 1,
  b: "some string",
  anArray: [1, "some other string"]
};

const params = toQueryString(myObject); // 'a=1&b=some%20string&anArray[]=1&anArray[]=some%20other%20string'

About

A standalone js function to convert an object to a query string

Resources

Stars

Watchers

Forks

Packages

No packages published