Using Map Reduce to Parse Query Strings

location.query
location.search
export const parseQueryString = (string) => {
return string.slice(1).split("&")
.map((queryParam) => {
let kvp = queryParam.split("=")
return { key: kvp[0], value: kvp[1] }
})
.reduce((query, kvp) => {
query[kvp.key] = kvp.value
return query
}, {})
}
// location.search = "?date=2017-05-22&timeslot=13:00"
// queryParams = parseQueryString(location.search)
// queryParams = { date: "2017-05-22", timeslot: "13:00" }

--

--

Dev is love. Dev is life.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store