JavaScript: Replace the names of multiple object keys with the values provided
JavaScript fundamental (ES6 Syntax): Exercise-14 with Solution
Replace Multiple Object Keys
Write a JavaScript program to replace multiple object keys' names with the values provided.
- 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.
Sample Solution:
JavaScript Code:
Output:
"Original Object" [object Object] { job: "Programmer", name: "Bobo", shoeSize: 100 } "-------------------------------------" "New Object" [object Object] { Actor: "Programmer", firstName: "Bobo", shoeSize: 100 }
Flowchart:

Live Demo:
For more Practice: Solve these Related Problems:
- Write a JavaScript program that renames several keys in an object based on a mapping of old keys to new keys.
- Write a JavaScript function that iterates over an object and replaces its keys according to a provided replacement object.
- Write a JavaScript program that deep clones an object and renames specific keys without affecting the original object.
Go to:
PREV : String Length in Bytes.
NEXT : Min-Max Value of Array with Function.
Improve this sample solution and post your code through Disqus
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.