Menu

gitpiper

unzipWith javascript Code Snippet in 2024

arrayadvanced

Last Updated: 12 April 2024

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

  • Use Math.max(), Function.prototype.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) => 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]

javascript snippet similar to unzipWith For You in April 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️