Created
November 4, 2019 13:13
-
-
Save imjiten/a399c966569419ad5d17495097836ad3 to your computer and use it in GitHub Desktop.
Convert URL GET parameters to Javascript Object & back to URL params (used to remove duplicated when forming URL)
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
| function getCombineGetParams(url1, url2) { | |
| let keyValue = []; | |
| let url1Params = new URLSearchParams(url1); | |
| url1Params.forEach(function (value, key) { | |
| keyValue[key] = value; | |
| }); | |
| let url2params = new URLSearchParams(url2); | |
| url2params.forEach(function (value, key) { | |
| keyValue[key] = value; | |
| }); | |
| return keyValue; | |
| } | |
| function getUrlFromParams(params) { | |
| let url = []; | |
| for (const [key, value] of Object.entries(params)) { | |
| url.push(key + '=' + value); | |
| } | |
| return '?' + url.join('&'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment