Generates an object from the given query string or URL.
String.prototype.split()
to get the params from the given url
.URLSearchParams
constructor to create an appropriate object and convert it to an array of key-value pairs using the spread operator (...
).Array.prototype.reduce()
to convert the array of key-value pairs into an object.const queryStringToObject = url =>
[...new URLSearchParams(url.split('?')[1])].reduce(
(a, [k, v]) => ((a[k] = v), a),
{}
);
queryStringToObject('https://google.com?page=1&count=10');
// {page: '1', count: '10'}
JavaScript, Object
Generates a query string from the key-value pairs of the given object.
JavaScript, Object
Retrieves a set of properties indicated by the given selectors from an object.
JavaScript, Object
Creates a generator, that walks through all the keys of a given object.