Convert array to identity object

JavaScript, Array · Apr 16, 2023

Converts an array of values into an object with the same values as keys and values.

const toIdentityObject = arr => Object.fromEntries(arr.map(v => [v, v]));
toIdentityObject(['a', 'b']); // { a: 'a', b: 'b' }

Written by Angelos Chalaris

I'm Angelos Chalaris, a JavaScript software engineer, based in Athens, Greece. The best snippets from my coding adventures are published here to help others learn to code.

If you want to keep in touch, follow me on GitHub.

More like this

  • JSON to CSV

    Converts an array of objects to a comma-separated values (CSV) string that contains only the columns specified.

    JavaScript, Array · Oct 13, 2021

  • Array to object based on key

    Creates an object from an array, using the specified key and excluding it from each value.

    JavaScript, Array · Jun 27, 2021

  • Group array into object

    Associates properties to values, given array of valid property identifiers and an array of values.

    JavaScript, Array · Oct 22, 2020