Menu

gitpiper

queryStringToObject javascript Code Snippet in 2024

objectintermediate

Last Updated: 13 April 2024

Generates an object from the given query string or URL.

  • Use String.prototype.split() to get the params from the given url.
  • Use new URLSearchParams() to create an appropriate object and convert it to an array of key-value pairs using the spread operator (...).
  • Use Array.prototype.reduce() to convert the array of key-value pairs into an object.
const queryStringToObject = url => [...new URLSearchParams(url.split('?')[1])].reduce( (a, [k, v]) => ((a[k] = v), a), {} );
queryStringToObject('https://google.com?page=1&count=10'); // {page: '1', count: '10'}

javascript snippet similar to queryStringToObject For You in April 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️