-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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