QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Convert Object to Query String

Converts an object to a query string for use in URLs.

  • object
  • query string
  • url

Install

npx shadcn@latest add https://quicksnip.dev/r/to-query-string.json

This snippet is not verified yet. Review it before use, or help verify it.

Code

function toQueryString(obj) {
  return Object.entries(obj)
    .map(([key, value]) => encodeURIComponent(key) + '=' + encodeURIComponent(value))
    .join('&');
}

Usage

const params = { search: 'test', page: 1 };
toQueryString(params); // Returns: 'search=test&page=1'