≪ Today I learned.
RSS購読
    公開日
    タグ
    JavaScript
    著者
    ダーシノ

    ECMAScript Stage2.7のJoint Itelationを使う

    2024年6月にJoint ItelationがStage2.7になった。

    Joint Itelationは2つ以上のイテレータ(配列など)を同時にループしたいときに使える。

    Before

    const itr1 = [1,2,3,4]
    const itr2 = [5,6,7,8]
    
    for (let i = 0; i < itr1.length; i++) {
      for (let j = 0; j < itr2.length; j++) {
        const a = itr1[i]
        const b = itr2[j]
        console.log({ a, b })  // [1,5], [2,6], [3,7], [4,8]
      }
    }

    After

    const joint = Array.from(Iterator.zipToArrays([ itr1, itr2 ]))
    
    joint.forEach(zipped => {
      console.log(zipped)  // [1,5], [2,6], [3,7], [4,8]
    })