Q:

Write a JavaScript program to create an object with the same keys as the provided object and values generated by running the provided function for each value

0

Write a JavaScript program to create an object with the same keys as the provided object and values generated by running the provided function for each value.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

const mapValues = (obj, fn) =>
  Object.keys(obj).reduce((acc, k) => {
    acc[k] = fn(obj[k], k, obj);
    return acc;
  }, {});
const users = {
  fred: { user: 'fred', age: 40 },
  pebbles: { user: 'pebbles', age: 1 }
};
console.log(mapValues(users, u => u.age));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now