ES6 snippet - JavaScript - GET parameters - GET URL parameters

PHOTO EMBED

Wed Nov 04 2020 08:47:54 GMT+0000 (Coordinated Universal Time)

Saved by @julien #javascript #http #get #url

const getURLParameters = url =>
  (url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce(
    (a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a),
    {}
  );

// Examples
getURLParameters('http://url.com/page?n=Adam&s=Smith'); // {n: 'Adam', s: 'Smith'}
getURLParameters('google.com'); // {}
content_copyCOPY

https://medium.com/swlh/24-modern-es6-code-snippets-to-solve-practical-js-problems-3609f301859e