Creates an array of arrays, ungrouping the elements in an array produced by zip.
Math.max()
, Function.prototype.apply()
to get the longest subarray in the array, Array.prototype.map()
to make each element an array.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, Array
Creates an array of elements, ungrouping the elements in an array produced by zip and applying the provided function.
JavaScript, Array
Creates an array of elements, grouped based on their position in the original arrays.
JavaScript, Array
Creates an array of elements, grouped based on the position in the original arrays and using a function to specify how grouped values should be combined.