Zipping Arrays in JavaScript
The "zip" operation combines two or more arrays element-wise into a new array of arrays. This is a common operation in data processing and functional programming, allowing you to pair corresponding elements from different datasets. Your task is to implement a zipArrays function in JavaScript that takes two or more arrays as input and returns a new array where each element is an array containing the corresponding elements from the input arrays.
Problem Description
You need to create a JavaScript function called zipArrays that accepts a variable number of arrays as arguments. The function should iterate through the input arrays simultaneously, creating a new array where each element is a new array containing the elements at the same index from each of the input arrays. The iteration should stop when the shortest input array is exhausted.
Key Requirements:
- The function must accept a variable number of arrays as arguments.
- The function must return a new array.
- Each element of the returned array should be a new array containing the corresponding elements from the input arrays.
- The iteration should stop when the shortest input array is exhausted.
- The function should handle empty input arrays gracefully.
Expected Behavior:
The function should return an empty array if any of the input arrays are empty. If all input arrays are empty, it should also return an empty array.
Edge Cases to Consider:
- Empty input arrays.
- Arrays of different lengths.
- A single input array.
- No input arrays.
Examples
Example 1:
Input: zipArrays([1, 2, 3], ['a', 'b', 'c'])
Output: [[1, 'a'], [2, 'b'], [3, 'c']]
Explanation: The function pairs the elements at the same index from the two input arrays.
Example 2:
Input: zipArrays([1, 2], ['a', 'b', 'c'], [true, false])
Output: [[1, 'a', true], [2, 'b', false]]
Explanation: The function pairs elements from three arrays, stopping when the shortest array (length 2) is exhausted.
Example 3:
Input: zipArrays([1, 2, 3], [])
Output: []
Explanation: Since one of the input arrays is empty, the function returns an empty array.
Example 4:
Input: zipArrays([1])
Output: [[1]]
Explanation: A single array is zipped into an array containing a single array with the element.
Constraints
- The input arrays can contain elements of any data type.
- The function must handle a maximum of 10 input arrays. (This is to prevent excessive memory usage with a very large number of arrays).
- The length of each input array can be up to 1000 elements.
- The function should have a time complexity of O(n), where n is the length of the shortest input array.
Notes
Consider using the arguments object or the spread operator (...) to handle a variable number of arguments. Think about how to efficiently iterate through the arrays simultaneously. Remember to handle the edge case where one or more arrays are empty. A for loop or forEach can be useful for iterating, but consider how to stop the iteration when the shortest array is exhausted.