Rename 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 rename object keys in JavaScript. The lab will cover how to use Object.keys() and Array.prototype.reduce() in combination with the spread operator to get an object's keys and rename them according to a provided mapping. By the end of the lab, you will have a solid understanding of how to effectively rename object keys in your JavaScript code.


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`") javascript/AdvancedConceptsGroup -.-> javascript/spread_rest("`Spread and Rest Operators`") subgraph Lab Skills javascript/variables -.-> lab-28591{{"`Rename Object Keys`"}} javascript/data_types -.-> lab-28591{{"`Rename Object Keys`"}} javascript/arith_ops -.-> lab-28591{{"`Rename Object Keys`"}} javascript/comp_ops -.-> lab-28591{{"`Rename Object Keys`"}} javascript/array_methods -.-> lab-28591{{"`Rename Object Keys`"}} javascript/higher_funcs -.-> lab-28591{{"`Rename Object Keys`"}} javascript/destr_assign -.-> lab-28591{{"`Rename Object Keys`"}} javascript/spread_rest -.-> lab-28591{{"`Rename Object Keys`"}} end

How to Rename Object Keys in JavaScript

To rename multiple object keys with the values provided, you can use the renameKeys function. Here are the steps you need to follow:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Use Object.keys() in combination with Array.prototype.reduce() and the spread operator (...) to get the object's keys and rename them according to keysMap.
  3. Pass the keysMap and object (obj) as arguments to the renameKeys function.
  4. The renameKeys function returns a new object with the renamed keys.

Here's an example of how to use the renameKeys function:

const renameKeys = (keysMap, obj) =>
  Object.keys(obj).reduce(
    (acc, key) => ({
      ...acc,
      ...{ [keysMap[key] || key]: obj[key] }
    }),
    {}
  );

const obj = { name: "Bobo", job: "Front-End Master", shoeSize: 100 };
renameKeys({ name: "firstName", job: "passion" }, obj);
// { firstName: 'Bobo', passion: 'Front-End Master', shoeSize: 100 }

Summary

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

Other JavaScript Tutorials you may like