To answer the original question: there’s no way to map over multiple arrays, so you have to rely on the index.
Here is a basic implementation:
function mmap(f, array1, ...rest_arrays) {
return array1.map((value, index) => f(value, ...rest_arrays.map(x => x[index])));
}
console.log(mmap((name, age) => `${name}: ${age}`,
['Alice', 'Bob'],
[32, 21]));
// => ["Alice: 32", "Bob: 21"]
I’m not 100% sure how to type this properly in TypeScript, but if all arrays have the same type that’s easier:
// T1: type of the elements of the arrays
// T2: type of the return value of the function
function mmap<T1, T2>(f: (x: T1, ...rest: T1[]) => T2, array1: T1[], ...rest_arrays: T1[][]): T2[] {
return array1.map((value, index) => f(value, ...rest_arrays.map(x => x[index])));
}
Then you can use it to implement zip: mmap(Array, ...arrays):
const arr1 = ['a', 'b', 'c'];
const arr2 = [1, 2, 3];
console.log(mmap(Array, arr1, arr2));
// => [ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ] ]
// It works on any number of arrays:
console.log(mmap(Array, arr1, arr2, arr1, arr2));
// => [ [ 'a', 1, 'a', 1 ], [ 'b', 2, 'b', 2 ], [ 'c', 3, 'c', 3 ] ]