Skip to content

Instantly share code, notes, and snippets.

@Octagon-simon
Created June 3, 2022 02:08
Show Gist options
  • Select an option

  • Save Octagon-simon/158689dfc42e903dd8bea5db79719164 to your computer and use it in GitHub Desktop.

Select an option

Save Octagon-simon/158689dfc42e903dd8bea5db79719164 to your computer and use it in GitHub Desktop.
function doQuery(url, userQuery){
//construct a new url
let res = new URL(url);
//check if userquery contians properties
if(Object.keys(userQuery).length !== 0){
//loop through object
let ind = 0;
while (ind < Object.keys(userQuery).length){
//get the parameter
const param = Object.keys(userQuery)[ind];
//get the value
const value = userQuery[param];
//set or replace the parameter
res.searchParams.set(param, value);
//increment counter
ind++;
}
}
//return the full URL
return ( res.href );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment