Skip to content

Instantly share code, notes, and snippets.

@RafaRochaS91
Last active September 25, 2019 15:38
Show Gist options
  • Select an option

  • Save RafaRochaS91/162c95f03e0ecaad2f5741f7c839f106 to your computer and use it in GitHub Desktop.

Select an option

Save RafaRochaS91/162c95f03e0ecaad2f5741f7c839f106 to your computer and use it in GitHub Desktop.
Query String Extract
// window.location.href => returns url
/**
* Objectivo é do url retornar:
*
* um objecto = { key:[...value], ... }
*
* exemplo: url => https://github.com/?foo=1,2&bar=1,2
* fn returns => { foo:[1,2],bar:[1,2] }
*/
/**
*
*/
function getKeyValue(url){
let keyValObj = {};
//after /?
let urlFullQuery = url.split('?')[1];
//after array with: [query1, query2, ...]
let singleQueriesArray = urlFullQuery.split('&');
//runs every query to separate key and value
for(const query of singleQueriesArray){
let valuesArray = [];
let queryValue = query.split('=')[1].split(',');
for(let q of queryValue){
queryValueInt = parseInt(q);
valuesArray.push(queryValueInt);
}
keyValObj[query.split('=')[0]] = valuesArray;
}
console.log(keyValObj);
};
getKeyValue('http://github.com/?value=1,2,3,4&value2=3,4,5');
@RafaRochaS91
Copy link
Copy Markdown
Author

This is extremely low performant, a better version to be included soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment