Unzip rows back into columns
A table comes back from the server as rows. Your chart wants columns: all the x values in one array, all the y values in another. That is a transpose.
Implement unzip(rows) where rows is an array of arrays. Return an array of columns: column j contains the element at index j from each row, in row order. The result has as many columns as the longest row. Where a shorter row has no element at that index, use null. An empty rows array returns an empty array. Applying unzip twice to a rectangular input gives back the original structure.
Arguments
rowsArray<Array>- A list of rows to transpose into columns.
Returns
Array<Array> — One array per column of the widest row, each in row order. Missing entries become null.
Examples
unzip([[1, "a"], [2, "b"]]);
// [[1, 2], ["a", "b"]]unzip([[1, 2], [3]]); // [[1, 3], [2, null]] — ragged rows pad with null
unzip(unzip([[1, 2], [3, 4]])); // [[1, 2], [3, 4]] — round-trips a rectangleWhat it has to do
- Produce one column per index of the longest row.
- Keep values in row order within each column.
- Fill gaps from shorter rows with
null. - Return
[]for an empty input. - Round-trip correctly: unzipping a rectangular result twice returns the original.
Stuck? The javascript course covers everything this challenge needs.
- 1.transposes a rectangular table
- 2.pads ragged rows with null
- 3.turns a single row into single-element columns
- 4.returns an empty array for no rows
- 5.round-trips a rectangular table
These assertions run in your browser the moment you hit Run. Pass all of them to claim the XP.
Discussion
Sign in to postNo comments yet. Be the first to say something.