mapValues and mapKeys are Array.prototype.map for objects. mapValues transforms every value while keeping the keys; mapKeys transforms every key while keeping the values. Both return a new object with the same number of entries, just reshaped.
Implement both. Each takes (obj, iteratee), where iteratee(value, key) produces the new value (for mapValues) or the new key (for mapKeys). Return a new object; don't mutate the input.
function mapValues(obj, iteratee) {} // same keys, iteratee(value, key) as each value
function mapKeys(obj, iteratee) {} // same values, iteratee(value, key) as each key
mapValues({ a: 1, b: 2 }, (v) => v * 10); // { a: 10, b: 20 }
mapKeys({ a: 1, b: 2 }, (v, k) => k.toUpperCase()); // { A: 1, B: 2 }
// The iteratee gets both value and key:
mapValues({ a: 1 }, (v, k) => `${k}=${v}`); // { a: 'a=1' }
mapKeys({ a: 1, b: 2 }, (v) => `k${v}`); // { k1: 1, k2: 2 }
mapValues keeps keys; mapKeys keeps values — each changes exactly one axis.(value, key), in that order (value first), matching lodash.mapKeys, the later entry wins.The full solution is part of Premium
Walkthrough, edge cases, complexity notes, and the runnable editor unlock with a Premium subscription.
Submissions are part of Premium
Unlock community code, comments, reactions, and framework-specific approaches.
mapValues and mapKeys are Array.prototype.map for objects. mapValues transforms every value while keeping the keys; mapKeys transforms every key while keeping the values. Both return a new object with the same number of entries, just reshaped.
Implement both. Each takes (obj, iteratee), where iteratee(value, key) produces the new value (for mapValues) or the new key (for mapKeys). Return a new object; don't mutate the input.
function mapValues(obj, iteratee) {} // same keys, iteratee(value, key) as each value
function mapKeys(obj, iteratee) {} // same values, iteratee(value, key) as each key
mapValues({ a: 1, b: 2 }, (v) => v * 10); // { a: 10, b: 20 }
mapKeys({ a: 1, b: 2 }, (v, k) => k.toUpperCase()); // { A: 1, B: 2 }
// The iteratee gets both value and key:
mapValues({ a: 1 }, (v, k) => `${k}=${v}`); // { a: 'a=1' }
mapKeys({ a: 1, b: 2 }, (v) => `k${v}`); // { k1: 1, k2: 2 }
mapValues keeps keys; mapKeys keeps values — each changes exactly one axis.(value, key), in that order (value first), matching lodash.mapKeys, the later entry wins.The full solution is part of Premium
Walkthrough, edge cases, complexity notes, and the runnable editor unlock with a Premium subscription.
Submissions are part of Premium
Unlock community code, comments, reactions, and framework-specific approaches.
Unlock the solution & editor
Runnable editor + tests
Solve in the browser with instant Jest feedback.
Detailed solutions
Walkthroughs, edge cases, and complexity notes.
Multi-framework variants
React, Vue, Vanilla, Angular — same question, different stacks.