Tried to find how to make {foo:\"bar\"}
from ?...&foo=bar&...
but googled and got only to jQuery.params
which does the opposit
for simple and flat query strings, something like this will do the trick
const queryStringToObject = (queryString) => {
let obj = {}
if(queryString) {
queryString.slice(1).split('&').map((item) => {
const [ k, v ] = item.split('=')
v ? obj[k] = v : null
})
}
return obj
}
> queryStringToObject('?foo=bar&baz=buzz')
{ foo: 'bar', baz: 'buzz' }