Hone logo
Hone
Problems

Unzip Arrays in JavaScript

This challenge focuses on a common data manipulation task: transforming an array of arrays into multiple arrays, effectively "unzipping" the data. This is incredibly useful when you have data organized into rows (e.g., from a CSV file or database query) and need to process columns individually.

Problem Description

Your task is to write a JavaScript function unzipArray that takes a single argument: an array of arrays. This input array represents a collection of "zipped" data, where each inner array has the same length. The function should return a new array of arrays, where each inner array contains elements from the corresponding position across all input inner arrays.

Key Requirements:

  • The function must accept an array of arrays as input.
  • The function must return a new array of arrays.
  • Each inner array in the output should contain elements from the same index across all input inner arrays.
  • The order of elements within the output inner arrays should match the order of the input inner arrays.

Expected Behavior:

If the input is [[1, 'a'], [2, 'b'], [3, 'c']], the output should be [[1, 2, 3], ['a', 'b', 'c']].

Edge Cases to Consider:

  • Empty Input Array: What should happen if the input array is empty ([])?
  • Empty Inner Arrays: What if the input array contains empty inner arrays ([[], []])?
  • Mismatched Inner Array Lengths: While the prompt implies consistent lengths, consider what would happen if the input contained inner arrays of different lengths. (For this challenge, we will assume all inner arrays have the same length, but it's good to be aware of this).

Examples

Example 1:

Input: [[1, 'a'], [2, 'b'], [3, 'c']]
Output: [[1, 2, 3], ['a', 'b', 'c']]
Explanation: The first output array [1, 2, 3] contains the first element from each input inner array. The second output array ['a', 'b', 'c'] contains the second element from each input inner array.

Example 2:

Input: [['apple', 100], ['banana', 200], ['cherry', 300]]
Output: [['apple', 'banana', 'cherry'], [100, 200, 300]]
Explanation: The first output array contains all the fruits, and the second output array contains their corresponding quantities.

Example 3:

Input: []
Output: []
Explanation: An empty input array should result in an empty output array.

Example 4:

Input: [[1], [2], [3]]
Output: [[1, 2, 3]]
Explanation: When each inner array has only one element, the output will be a single array containing all those elements.

Constraints

  • The input will always be an array.
  • The elements of the input array will always be arrays themselves.
  • For this challenge, assume all inner arrays within the input array will have the same length.
  • The maximum number of inner arrays in the input is 1000.
  • The maximum length of each inner array is 1000.

Notes

  • Think about how to iterate through the input and construct the output structure.
  • Consider using built-in array methods that might simplify the process.
  • The goal is to create a clean and efficient solution.
Loading editor...
javascript