Javascript ES6 - map multiple arrays

后端 未结 3 1724
甜味超标
甜味超标 2021-01-07 22:23

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         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-07 23:12

    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]
    

提交回复
热议问题