1

Invert an object

 1 year ago
source link: https://www.reacterry.com/portal/challenges/invert-an-object
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Intermediate
JavaScript

Invert an object

Write a function invertObject(obj), which takes an object obj as an argument and returns a new object where the keys and values of the original object are swapped.

Directions

  • The values of the input object can only be strings and numbers
  • If the input object is empty, then return an empty object
  • The function should not modify the original object
  • The function should handle objects with any number of key-value pairs

Example

const originalObj = {
name: 'John',
age: 30,
city: 'New York'
const invertedObj = invertObject(originalObj);
console.log(invertedObj); // Output: { John: 'name', '30': 'age', 'New York': 'city' }
Hint 1

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK