Parse a query string in JavaScript
Pulls a query string apart into an object, repeated keys included.
function parseQuery(search) {
const params = {};
for (const pair of search.replace(/^\?/, "").split("&")) {
if (!pair) continue;
const [key, value = ""] = pair.split("=");
params[decodeURIComponent(key)] = decodeURIComponent(value);
}
return params;
}
Keywords and builtins used here
constcontinuedecodeURIComponentforfunctionifofreturn
The run, in numbers
- Lines
- 9
- Characters to type
- 251
- Tokens
- 73
- Three-star pace
- 95 tpm
At the three-star pace of 95 tokens a minute, this run takes about 46 seconds.
Step 3 of 3 in Data formats, step 42 of 43 in Language basics.