30-seconds-of-code
Version:
A collection of useful JavaScript snippets.
14 lines (9 loc) • 296 B
Markdown
### objectFromPairs
Creates an object from the given key-value pairs.
Use `Array.reduce()` to create and combine key-value pairs.
```js
const objectFromPairs = arr => arr.reduce((a, [key, val]) => ((a[key] = val), a), {});
```
```js
objectFromPairs([['a', 1], ['b', 2]]); // {a: 1, b: 2}
```