Files
30-seconds-of-code/javascript/snippets/to-identity-object.md
2023-05-01 22:35:56 +03:00

557 B

title, type, tags, author, cover, dateModified
title type tags author cover dateModified
Convert array to identity object snippet
array
chalarangelo rain-shopping 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.
const toIdentityObject = arr => Object.fromEntries(arr.map(v => [v, v]));
toIdentityObject(['a', 'b']); // { a: 'a', b: 'b' }