Skip to content

Instantly share code, notes, and snippets.

@imjiten
Created November 4, 2019 13:13
Show Gist options
  • Select an option

  • Save imjiten/a399c966569419ad5d17495097836ad3 to your computer and use it in GitHub Desktop.

Select an option

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)
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