Skip to content

Instantly share code, notes, and snippets.

@CanRau
Forked from pirate/parseURLParameters.js
Last active March 20, 2018 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CanRau/3045c6dfef4a94eac6541759810d7c63 to your computer and use it in GitHub Desktop.
Save CanRau/3045c6dfef4a94eac6541759810d7c63 to your computer and use it in GitHub Desktop.
Parse URL query parameters in ES6 (immutable version using reduce instead of map)
const getUrlParams = (search = ``) => {
const hashes = search.slice(search.indexOf(`?`) + 1).split(`&`)
return hashes.reduce((acc, hash) => {
const [key, val] = hash.split(`=`)
return {
...acc,
val: decodeURIComponent(val)
}
}, {})
}
console.log(getUrlParams(window.location.search))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment