Is there a feature in JavaScript 6 that allows to map over multiple arrays ?
Something like a zipper :
var myFn = function (a, b) { console.log(a, b
You could also use reduce
to get the desired outcome:
var arr1 = ['a', 'b', 'c'];
var arr2 = [1, 2, 3];
arr1.reduce((acc, current, index) => {
console.log(current, arr2[index])
return [...acc, current, arr2[index]]
}, [])
// a 1
// b 2
// c 3
// returns ["a", 1, "b", 2, "c", 3]
As the other answer points out, this is commonly known as a zip
. It can be implemented as:
let zipped = arr1.map((x, i) => [x, arr2[i]]);
Or as a function, basically:
let zip = (a1, a2) => a1.map((x, i) => [x, a2[i]]);
Which would let you do:
zip(["a","b","c"], [1,2,3]); // ["a", 1], ["b", 2], ["c", 3]
Unfortunately, no. What you are looking for is commonly called zip
or zipWith
. See lodash's implementation for a reference: https://lodash.com/docs#zipWith