Invert Object Key-Value Pairs

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore how to invert an object's key-value pairs without mutating the original object. We will use the Object.keys() method and Array.prototype.reduce() to invert the object. Additionally, we will learn how to apply a function to the inverted keys and return an array of keys responsible for generating the inverted value.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/array_methods("`Array Methods`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") javascript/AdvancedConceptsGroup -.-> javascript/destr_assign("`Destructuring Assignment`") subgraph Lab Skills javascript/variables -.-> lab-28404{{"`Invert Object Key-Value Pairs`"}} javascript/data_types -.-> lab-28404{{"`Invert Object Key-Value Pairs`"}} javascript/arith_ops -.-> lab-28404{{"`Invert Object Key-Value Pairs`"}} javascript/comp_ops -.-> lab-28404{{"`Invert Object Key-Value Pairs`"}} javascript/array_methods -.-> lab-28404{{"`Invert Object Key-Value Pairs`"}} javascript/higher_funcs -.-> lab-28404{{"`Invert Object Key-Value Pairs`"}} javascript/destr_assign -.-> lab-28404{{"`Invert Object Key-Value Pairs`"}} end

Function to Invert an Object

To invert the key-value pairs of an object without altering the original object, use the invertKeyValues function.

  • Call the function by typing invertKeyValues(obj, fn) in the Terminal/SSH, where obj is the object to be inverted and fn is an optional function to be applied to the inverted key.

  • The Object.keys() and Array.prototype.reduce() methods are used to create a new object with inverted key-value pairs, and if a function is provided, it is applied to each inverted key.

  • If fn is omitted, the function returns only the inverted keys without any function applied to them.

  • The function returns an object with each inverted value being an array of keys responsible for generating the inverted value.

const invertKeyValues = (obj, fn) =>
  Object.keys(obj).reduce((acc, key) => {
    const val = fn ? fn(obj[key]) : obj[key];
    acc[val] = acc[val] || [];
    acc[val].push(key);
    return acc;
  }, {});

Example usage:

invertKeyValues({ a: 1, b: 2, c: 1 }); // { 1: [ 'a', 'c' ], 2: [ 'b' ] }
invertKeyValues({ a: 1, b: 2, c: 1 }, (value) => "group" + value);
// { group1: [ 'a', 'c' ], group2: [ 'b' ] }

Summary

Congratulations! You have completed the Invert Object lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like