Javascript equivalent of Python's zip function

前端 未结 18 1635
天命终不由人
天命终不由人 2020-11-21 07:40

Is there a javascript equivalent of Python\'s zip function? That is, given multiple arrays of equal lengths create an array of pairs.

For instance, if I have three

18条回答
  •  广开言路
    2020-11-21 07:55

    If you are fine with ES6:

    const zip = (arr,...arrs) =>(
                                arr.map(
                                  (v,i) => arrs.reduce((a,arr)=>[...a, arr[i]], [v])))
    

提交回复
热议问题