Q:

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

0

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

All Answers

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

const mapKeys = (obj, fn) =>
  Object.keys(obj).reduce((acc, k) => {
    acc[fn(obj[k], k, obj)] = obj[k];
    return acc;
  }, {});
console.log(mapKeys({ a: 1, b: 2 }, (val, key) => key + val));

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