Deep Map Object Keys

JavaScriptJavaScriptBeginner
Practice Now

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

Introduction

In this lab, we will explore how to use the deepMapKeys function in JavaScript to map an object's keys recursively. We will learn how to create a new object with the same values and mapped keys using a provided function for each key. Through hands-on exercises and examples, we will understand how this function works and how it can be used in practical scenarios.


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`") subgraph Lab Skills javascript/variables -.-> lab-28265{{"`Deep Map Object Keys`"}} javascript/data_types -.-> lab-28265{{"`Deep Map Object Keys`"}} javascript/arith_ops -.-> lab-28265{{"`Deep Map Object Keys`"}} javascript/comp_ops -.-> lab-28265{{"`Deep Map Object Keys`"}} javascript/array_methods -.-> lab-28265{{"`Deep Map Object Keys`"}} javascript/higher_funcs -.-> lab-28265{{"`Deep Map Object Keys`"}} end

Deep Map Object Keys

To deep map an object's keys, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Use the deepMapKeys function with the provided object and a function that generates new keys.
  3. The function creates an object with the same values as the provided object and keys generated by running the provided function for each key.
  4. Iterate over the object's keys using Object.keys().
  5. Create a new object with the same values and mapped keys using Array.prototype.reduce() and the provided function.
  6. If a value is an object, recursively call deepMapKeys on it to map its keys as well.
const deepMapKeys = (obj, fn) =>
  Array.isArray(obj)
    ? obj.map((val) => deepMapKeys(val, fn))
    : typeof obj === "object"
      ? Object.keys(obj).reduce((acc, current) => {
          const key = fn(current);
          const val = obj[current];
          acc[key] =
            val !== null && typeof val === "object"
              ? deepMapKeys(val, fn)
              : val;
          return acc;
        }, {})
      : obj;

Here's an example usage of deepMapKeys:

const obj = {
  foo: "1",
  nested: {
    child: {
      withArray: [
        {
          grandChild: ["hello"]
        }
      ]
    }
  }
};

const upperKeysObj = deepMapKeys(obj, (key) => key.toUpperCase());
/*
{
  "FOO":"1",
  "NESTED":{
    "CHILD":{
      "WITHARRAY":[
        {
          "GRANDCHILD":[ 'hello' ]
        }
      ]
    }
  }
}
*/

Summary

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

Other JavaScript Tutorials you may like