Menu

gitpiper

unzip javascript Code Snippet in 2024

arrayintermediate

Last Updated: 9 March 2024

Creates an array of arrays, ungrouping the elements in an array produced by zip.

  • 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.
const unzip = arr => 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 => []) );
unzip([['a', 1, true], ['b', 2, false]]); // [['a', 'b'], [1, 2], [true, false]] unzip([['a', 1, true], ['b', 2]]); // [['a', 'b'], [1, 2], [true]]

javascript snippet similar to unzip For You in March 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! ✌️