Javascript equivalent of Python's zip function

前端 未结 18 1668
天命终不由人
天命终不由人 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:48

    I modified flm's nifty answer to take an arbitrary number of arrays:

    function* zip(arrays, i = 0) {
      while (arrays[0][i]) {
        yield arrays.map((arr, j) => arr[j < arrays.length - 1 ? i : i++])
      }
     }

提交回复
热议问题