'how to save data downloaded in a loop to json

I have a problem. I got one to save the data called in a loop to an empty json. It's about "eventsPolygon". One args with index 0 will have to be written to JSONA. How to do it?

async function main() {
    console.log("Start checking rewards")
    const currentBlockNumberPolygon = await maticProvider.getBlockNumber() - 1
    const currentBlockNumberBsc = await bscProvider.getBlockNumber() - 1

    const oldestBlockNumberPolygon = 22939848
    const oldestBlockNumberBsc = 13763979


    const eventFilterPolygon = Missions.filters.RewardToPay()
    const eventFilterBsc = Rewards.filters.RewardPayed()

    let eventsPolygon = []
    let eventsBsc = []

    
    


     for(let i = oldestBlockNumberPolygon; i < currentBlockNumberPolygon-10000; i+=10000) {
       const eventsLoop = await Missions.queryFilter(eventFilterPolygon, i, i+10000)
       eventsPolygon = eventsPolygon.concat(eventsLoop)
       console.log(i)

       

      }

    //for(let i = oldestBlockNumberBsc; i < currentBlockNumberBsc-10000; i+=10000) {
      //const eventsLoop = await Rewards.queryFilter(eventFilterBsc, i, i+10000)
     // eventsBsc = eventsBsc.concat(eventsLoop)
      //console.log(i)
    //}

  

    console.log('a')



}


Solution 1:[1]

when iterating if your certain that you need the zero index you could just make a condition inside your loop, if(i == 0){wanted action}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hen Moshe