Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • 1
    You should use a .reduce() instead of .forEach Commented Jan 16, 2018 at 14:49
  • 1
    Doesn't work with .reduce(). Only first param is written to params object. Checked in Chrome dev tools. Can you please double check and and replace with forEach() instead? Commented Jan 28, 2019 at 11:06
  • @vinyll @tylik const getQueryObject = locationSearchString => locationSearchString .substr(1) .split('&') .reduce((queryObject, keyValuePair) => { const [key, value] = keyValuePair.split('='); queryObject[key] = value; return queryObject; }, {}); Commented Feb 20, 2019 at 10:03
  • 1
    you should use URL but if you're going to do it this way you need to call decodeURIComponent on every key and value after splitting by & and =. This is how it's possible to encode = and & in the keys and values themselves as well as other things like # as they'll all be encoded. Commented Jan 31, 2020 at 7:05