Swap the Keys and Values in an Object using JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 23, 2022

banner

Photo from Unsplash

Swap the Keys and Values in an Object #

To swap the keys and values in an object:

  1. Call the Object.entries() method to get an array of key-value pairs
  2. Use the map() method to switch the places of the key and the value
  3. Pass the result to the Object.fromEntries() method to get an object with swapped keys and values.
index.js
function swapKeysAndValues(obj) { // 👇️ [['color', 'blue'], ['fruit', 'apple']] const swapped = Object.entries(obj).map( ([key, value]) => [value, key] ); return Object.fromEntries(swapped); } // 👇️ {color: 'blue', fruit: 'apple'} console.log( swapKeysAndValues({blue: 'color', apple: 'fruit'}) );

The first step is to use the Object.entries method to get an array of the object's key-value pairs.

index.js
// 👇️ [['blue', 'color'], ['apple', 'fruit']] console.log(Object.entries({blue: 'color', apple: 'fruit'}))
The first element in each nested array is the key, and the second is the value.

We used the Array.map method to swap the places of the key and the value in the arrays.

The function we passed to the map() method gets called with each element (sub-array) in the array.

We used destructuring assignment to get the first and second elements of each sub-array.

index.js
const [key, value] = ['color', 'blue']; console.log(key); // 👉️ "color" console.log(value); // 👉️ "blue"

We simply switch the order of the key and value and return a new array containing the result.

The final value the swapped variable contains is an array of arrays, where the values are at index 0 and the keys are at index 1.

The last step is to pass the array to the Object.fromEntries method.

The Object.fromEntries method takes an array of key-value pairs, adds them to an object and returns the result.

index.js
// 👇️ {blue: 'color', apple: 'fruit'} console.log( Object.fromEntries([ ['blue', 'color'], ['apple', 'fruit'], ]), );

To swap the keys and the values of an object in 3 steps:

  1. Get an array of key-value pairs using the Object.entries() method.
  2. Use the map() method to swap the place of each key and value.
  3. Use the Object.fromEntries() method to transform the key-value pair arrays to an object.

An alternative approach is to return an array of objects from the map() method.

index.js
function swapKeysAndValues(obj) { // 👇️ [{color: 'blue'}, {fruit: 'apple'}] const swapped = Object.entries(obj).map( ([key, value]) => ({[value]: key}) ); return Object.assign({}, ...swapped); } // 👇️ {color: 'blue', fruit: 'apple'} console.log(swapKeysAndValues({blue: 'color', apple: 'fruit'}));

We return an array of objects from the map() method. Again, we switch the key and the value for each pair.

This allows us to use the Object.assign method to copy the properties of all objects into a single object.

We used the spread syntax (...) to unpack the values (objects) from the array and passed them as arguments to the Object.assign method.

The first parameter the Object.assign method takes is a target object, and the next is source object(s). The key-value pairs of the source objects get copied to the target object.

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.