Files
30-seconds-of-code/snippets/objectFromPairs.md
Angelos Chalaris 611729214a Snippet format update
To match the starter (for the migration)
2019-08-13 10:29:12 +03:00

16 lines
344 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}
```