Overview
Implement objectMap(object, fn) that returns a new object with the same keys but values transformed by fn(value, key).
Examples
objectMap({ a: 1, b: 2 }, v => v * 2); // { a: 2, b: 4 }Solution
Reveal solution
function objectMap(object, fn) {
const r = {};
for (const [k, v] of Object.entries(object)) r[k] = fn(v, k);
return r;
}object-map.js
Object Map
easycodingJavaScriptObjects
Overview
Implement objectMap(object, fn) that returns a new object with the same keys but values transformed by fn(value, key).
Examples
objectMap({ a: 1, b: 2 }, v => v * 2); // { a: 2, b: 4 }Solution
Reveal solution
function objectMap(object, fn) {
const r = {};
for (const [k, v] of Object.entries(object)) r[k] = fn(v, k);
return r;
}NameTopicDifficulty
103 of 103 problems