Q:

Write a JavaScript program to apply a function against an accumulator and each key in the object (from left to right)

0

Write a JavaScript program to apply a function against an accumulator and each key in the object (from left to right). 

All Answers

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

const transform = (obj, fn, acc) => Object.keys(obj).reduce((a, k) => fn(a, obj[k], k, obj), acc);
console.log(transform(
  { a: 1, b: 2, c: 1 },
  (r, v, k) => {
    (r[v] || (r[v] = [])).push(k);
    return r;
  },
  {}
));

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