Last active
September 25, 2019 15:38
-
-
Save RafaRochaS91/162c95f03e0ecaad2f5741f7c839f106 to your computer and use it in GitHub Desktop.
Query String Extract
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
| // 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'); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is extremely low performant, a better version to be included soon.