Q:

Write a JavaScript program to create an array of elements, ungrouping the elements in an array produced by zip and applying the provided function

0

Write a JavaScript program to create an array of elements, ungrouping the elements in an array produced by zip and applying the provided function.

All Answers

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

const unzipWith = (arr, fn) =>
  arr
    .reduce(
      (acc, val) => (val.forEach((v, i) => acc[i].push(v)), acc),
      Array.from({
        length: Math.max(...arr.map(x => x.length))
      }).map(x => [])
    )
    .map(val => fn(...val));

console.log(unzipWith([[1, 10, 100], [2, 20, 200]], (...args) => args.reduce((acc, v) => acc + v, 0)));

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