A few suggestions - consider using window.location.search
to access the query string directly.
Also, you might want to factor in scenarios where "arrays" exist in your query string (ie multiple values sharing the same key). One way to handle that might be to return an array of values found for a key, in the data
object that you return.
For more detail, see the comments in this updated version of the function:
function parseQueryString() {
// Use location.search to access query string instead
const qs = window.location.search.replace('?', '');
const items = qs.split('&');
// Consider using reduce to create the data mapping
return items.reduce((data, item) => {
const [rawKey, rawValue] = item.split('=');
const key = decodeURIComponent(rawKey);
const value = decodeURIComponent(rawValue);
// Sometimes a query string can have multiple values
// for the same key, so to factor that case in, you
// could collect an array of values for the same key
if(data[key] !== undefined) {
// If the value for this key was not previously an
// array, update it
if(!Array.isArray(data[key])) {
data[key] = [ data[key] ]
}
data[key].push(value)
}
else {
data[key] = value
}
return data
}, {})
}