Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Invert Keys and Values

Data Structures, Operators & Stringseasy

Course · Section 9: Data Structures, Modern Operators and Strings · Lecture 118: Enhanced Object Literals

Implement `invert(obj)` returning a new object where each value becomes a key and each key becomes the value.

Sample tests

Input: invert({"a":1,"b":2})
Output: {"1":"a","2":"b"}
Input: invert({})
Output: {}

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Duplicate values collide, with later keys overwriting earlier ones.
  • Numeric values become string keys on the result object.

Learning resources

  • MDN: Object
Approach & explanation (try first)

Swapping each entry's key and value builds the inverted object; remember object keys are always strings.

Loading...
⌘/Ctrl + Enter

Run your code to see results.