Files
30-seconds-of-code/snippets/objectFromPairs.md
Isabelle Viktoria Maciohsek ce48fffb4d Update snippet descriptions
2020-10-21 21:54:53 +03:00

18 lines
349 B
Markdown

---
title: objectFromPairs
tags: object,array,beginner
---
Creates an object from the given key-value pairs.
- Use `Array.prototype.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}
```