unzipWith
Creates an array of elements, ungrouping the elements in an array produced by zip
and applying the provided function.
Note: Provide size to make calculation faster
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: any[], fn: Function, size = 0) =>
arr
.reduce(
(acc, val) => (val.forEach((v: any, i: number) => acc[i].push(v)), acc),
Array.from({
length: size || Math.max(...arr.map((x) => x.length)),
}).map((x) => [])
)
.map((val: any) => fn(...val));
typescript
unzipWith(
[
[1, 10, 100],
[2, 20, 200],
],
(...args) => args.reduce((acc, v) => acc + v, 0)
); // [3, 30, 300]
unzipWith(
[
[1, 10, 100],
[2, 20, 200],
],
(...args) => args.reduce((acc, v) => acc + v, 0),
3
); // [3, 30, 300]