Skip to curriculum
Jump to the editor

5.1. Map an object's values

Easy
Topics
10–20 min

Arrays have map; objects do not. Yet the need is everywhere: prices keyed by product that must be shown with tax, settings keyed by name that must be turned into labels, an API response keyed by id that must be reshaped. mapValues keeps the keys and transforms each value.

Write mapValues(object, fn) in map-values.js. It returns a new plain object with the same keys as object, where each value is fn(value, key, object).

Signature

JAVASCRIPT
function mapValues(object, fn) // returns a new plain object
  • object: the object to read. Its own enumerable string keys are used, the ones Object.keys lists.
  • fn(value, key, object): called once per key; its result is the new value.
  • Returns a new object whose prototype is Object.prototype. The input is not changed.

Requirements

  1. Each key of the result holds fn(value, key, object) for that key; fn receives the value, the key and the object itself, once per key.
  2. The result is a new object, and the input is unchanged.
  3. The keys keep their order: the same order as Object.keys of the input (integer-like keys first, ascending, then the others in the order they were added).
  4. Only own enumerable string keys count: inherited properties, non-enumerable properties and symbol keys are left out.
  5. An empty object gives an empty object, and a key whose new value is undefined is still there.
  6. A key named __proto__ (as JSON.parse can produce) is an ordinary key in the result; the result's prototype stays Object.prototype.
  7. The result is a plain object even when the input is a class instance, an object without a prototype, or an array.
  8. Each value is read once.
  9. It throws a TypeError when fn is not a function (even for an empty object), and when object is null or undefined.

Examples

JAVASCRIPT
mapValues({ a: 1, b: 2 }, (value) => value * 10);   // { a: 10, b: 20 }
mapValues({ tea: 40, cake: 120 }, (price, item) => item + ": " + price); // { tea: "tea: 40", cake: "cake: 120" }
JAVASCRIPT
const child = Object.create({ inherited: 1 });
child.own = 2;
mapValues(child, (value) => value * 10);            // { own: 20 }: inherited keys are left out
JAVASCRIPT
const parsed = JSON.parse('{"__proto__": 1}');
mapValues(parsed, (value) => value + 1);            // an object with an own key "__proto__" holding 2
mapValues({ a: 1 }, "not a function");              // TypeError

Environment

  • Plain JavaScript in a browser page; the tests call the global mapValues from map-values.js.
  • Every built-in is allowed. Primitive inputs other than null and undefined (a string, a number) are out of scope.

Hints

  1. Which built-in lists exactly the own enumerable string keys, in order?
  2. What does result["__proto__"] = value do on a plain object, and how else can you create a property?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…