Transform query parameters into a Object with ES6

Being able to parse a URL into a native object is one of those things you'd expect in a standard library. Unfortunately, JavaScript is often lacking in that department.

There are a lot of implementations out there of this problem, of varying quality. I wasn't able to come across one that takes advantage of ES6 features. Here is what I came up with.

const paramObject = (queryString = window.location.search.substring(1)) =>  
queryString.split('&')  
  .map(str => {
    let [key, value] = str.split('=');
    return {[key]: decodeURI(value)};
  })
  .reduce((prev, curr) => Object.assign(prev, curr));

This takes care of a lot of things that I saw missing from other implementations. It allows you to override the default location and also takes care of values that are encoded.

Check it out with some tests at JSFiddle.

Bret Lowrey

Code is like a war - the best code is one never written.

Florida, USA