Parsing URL string with lodash

Es6 way without lodash :

  const urlParsed =
    rawUrl.substring(rawUrl.indexOf("?") + 1)
          .split("&")
          .reduce(
            (memo, param) => ({
              ...memo,
              [param.split("=")[0]]: param.split("=")[1]
            }),
            {}
          );

to avoid callback use _.partial, _.partialRight and other lodash methods to work with functions

_.chain(rawUrl)
    .replace('?', '') // a=b454&c=dhjjh&f=g6hksdfjlksd
    .split('&') // ["a=b454","c=dhjjh","f=g6hksdfjlksd"]
    .map(_.partial(_.split, _, '=', 2)) // [["a","b454"],["c","dhjjh"],["f","g6hksdfjlksd"]]
    .fromPairs() // {"a":"b454","c":"dhjjh","f":"g6hksdfjlksd"}
    .value()

You can also use https://github.com/sindresorhus/query-string instead of creating your own version of this feature.


I'm aware that you asked for a lodash solution, but maybe people who read the question might also want to look at URLSearchParams, it helped me using less code than the ES6 approach, and avoiding lodash.

const params = new URLSearchParams(window.location.search);

to access a query parameter called 'a'

let a = params.get('a');

https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams