HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Invert the key-value pairs of a JavaScript object

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
javascriptobjectpairsvaluetheinvertkey

Problem

While fairly uncommon, inverting the key-value pairs of an object can be useful in certain scenarios. In its simplest form, this is only possible if the values of the object are unique. This is because the values of the original object will become the keys of the inverted object, and if there are duplicate values, they will overwrite each other.
Given that the values of the object are unique, you can use Object.entries() to get an array of key-value pairs, Array.prototype.map() to reverse them, and Object.fromEntries() to create a new object from the inverted pairs.
If the values of the object contain duplicates, you'll have to handle them a little differently. Instead of overwriting the keys in the inverted object, you can append them to an array. This way, you can keep track of all the keys that share the same value.

Solution

const invertKeyValues = obj =>
  Object.fromEntries(
    Object.entries(obj).map(entry => entry.reverse())
  );

invertKeyValues({ a: 1, b: 2, c: 3 });
// { 1: 'a', 2: 'b', 3: 'c' }
invertKeyValues({ a: 1, b: 2, c: 1 });
// { 1: 'c', 2: 'b' }


If the values of the object contain duplicates, you'll have to handle them a little differently. Instead of overwriting the keys in the inverted object, you can append them to an array. This way, you can keep track of all the keys that share the same value.

Code Snippets

const invertKeyValues = obj =>
  Object.fromEntries(
    Object.entries(obj).map(entry => entry.reverse())
  );

invertKeyValues({ a: 1, b: 2, c: 3 });
// { 1: 'a', 2: 'b', 3: 'c' }
invertKeyValues({ a: 1, b: 2, c: 1 });
// { 1: 'c', 2: 'b' }
const invertKeyValues = obj =>
  Object.entries(obj).reduce((acc, [key, val]) => {
    acc[val] = acc[val] || [];
    acc[val].push(key);
    return acc;
  }, {});

invertKeyValues({ a: 1, b: 2, c: 1 });
// { 1: [ 'a', 'c' ], 2: [ 'b' ] }
invertKeyValues({ a: 1, b: 2, c: 1, d: 2 });
// { 1: [ 'a', 'c' ], 2: [ 'b', 'd' ] }

Context

From 30-seconds-of-code: invert-key-values

Revisions (0)

No revisions yet.