30 seconds of code

  Back to Home



Creates an array of elements, ungrouping the elements in an array produced by zip and applying the provided function.

Use Math.max.apply() to get the longest subarray in the array, Array.prototype.map() to make each element an array. Use Array.prototype.reduce() and Array.prototype.forEach() to map grouped values to individual arrays. Use Array.prototype.map() and the spread operator (...) to apply fn to each individual group of elements.

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