18 lines
523 B
TypeScript
Executable File

export function zipArrays<T, U>(array1: T[], array2: U[]): (T | U)[] {
const zippedArray: (T | U)[] = [];
const minLength = Math.min(array1.length, array2.length);
for (let i = 0; i < minLength; i++) {
zippedArray.push(array1[i], array2[i]);
}
// Append remaining elements of the longer array
if (array1.length > array2.length) {
zippedArray.push(...array1.slice(minLength));
} else if (array2.length > array1.length) {
zippedArray.push(...array2.slice(minLength));
}
return zippedArray;
}