Created
December 14, 2021 07:45
-
-
Save Samda/3118da774093edec73e5b8cdab578d05 to your computer and use it in GitHub Desktop.
Parse url params to object
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 parseParams = (querystring) => { | |
// parse query string | |
const params = new URLSearchParams(querystring); | |
const obj = {}; | |
// iterate over all keys | |
for (const key of params.keys()) { | |
if (params.getAll(key).length > 1) { | |
obj[key] = params.getAll(key); | |
} else { | |
obj[key] = params.get(key); | |
} | |
} | |
return obj; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment