Files
30-seconds-of-code/snippets/toIdentityObject.md
Angelos Chalaris 40ef1e41b0 Add 2 new snippets
2023-04-09 21:41:58 +03:00

21 lines
538 B
Markdown

---
title: Convert array to identity object
tags: array
author: chalarangelo
cover: rain-shopping
firstSeen: 2023-04-16T05:00:00-04:00
---
Converts an array of values into an object with the same values as keys and values.
- Use `Array.prototype.map()` to map each value to an array of key-value pairs.
- Use `Object.fromEntries()` to convert the array of key-value pairs into an object.
```js
const toIdentityObject = arr => Object.fromEntries(arr.map(v => [v, v]));
```
```js
toIdentityObject(['a', 'b']); // { a: 'a', b: 'b' }
```