DEV Community

Discussion on: 24 modern ES6 code snippets to solve practical JS problems

Collapse
 
webreflection profile image
Andrea Giammarchi • Edited

The getURLParameters is not really safe/useful, I'd go instead with:

const getURLParameters = url => [
  ...new URL(url).searchParams
].reduce(
  (obj, [key, value]) => ((obj[key] = value), obj),
  {}
);

or, for possible duplicated keys:

const getURLParameters = url => [
  ...new URL(url).searchParams
].reduce(
  (obj, [key, value]) => ((
    obj[key] = key in obj ? [].concat(obj[key], value) : value
  ), obj),
  Object.create(null)
);