Prepare repository for merge

This commit is contained in:
Angelos Chalaris
2023-05-01 22:35:56 +03:00
parent fc4e61e6fa
commit b3ad01863a
578 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,26 @@
---
title: Query string to object
type: snippet
tags: [object]
cover: dark-mode
dateModified: 2020-11-03T22:11:18+02:00
---
Generates an object from the given query string or URL.
- Use `String.prototype.split()` to get the params from the given `url`.
- Use the `URLSearchParams` constructor to create an appropriate object and convert it to an array of key-value pairs using the spread operator (`...`).
- Use `Array.prototype.reduce()` to convert the array of key-value pairs into an object.
```js
const queryStringToObject = url =>
[...new URLSearchParams(url.split('?')[1])].reduce(
(a, [k, v]) => ((a[k] = v), a),
{}
);
```
```js
queryStringToObject('https://google.com?page=1&count=10');
// {page: '1', count: '10'}
```