unzipWith() function | JavaScript

December 24th, 2021 07:51:08 PM

					
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)); unzipWith( [ [1, 10, 100], [2, 20, 200], ], (...args) => args.reduce((acc, v) => acc + v, 0) ); // [3, 30, 300]

Featured Posts