简体   繁体   English

如何在异步函数中访问双嵌套数组中的数据 - javascript?

[英]How to access data inside double nested array in async function - javascript?

I've got array which has another array which contains data i need.我有一个数组,它有另一个包含我需要的数据的数组。

How do i access "0x88def628c16651eb0d86be5ead3d738d0cb27fe947bb786c23105ac5d67a1bd0" in javascript for example?例如,我如何在 javascript 中访问“0x88def628c16651eb0d86be5ead3d738d0cb27fe947bb786c23105ac5d67a1bd0”? This is being displayed by calling var transakcije.这是通过调用 var transakcije 来显示的。

I've tried with:我试过:

transakcije[0][0] but that is not the name of sub array, transakcije[0][0] 但这不是子数组的名称,

transakcije[0], transakcije[0],

for loop (transakcija as transakcije) to no avail. for 循环(transakcija 为 transakcije)无济于事。

I've searched familiar answers but found none to my aid.我搜索了熟悉的答案,但没有找到对我有用的答案。 I appreciate any help i recieve.我感谢我收到的任何帮助。 Below is var transakcije being displayed in console.log().下面是显示在 console.log() 中的 var transakcije。


在此处输入图片说明


function getBlockchainTransactions(blockNumber){
    var tx = [];
    return new Promise(resolve => {
        for(var i=0; i<blockNumber; i++){
            web3.eth.getBlock(i, function(error, block){
            if(!error && block.transactions.length != 0){
                console.log(block.transactions);
                tx.push(block.transactions);
            }
        })
        }
            resolve(tx);
    });

}


async function msg() {
    const transakcije = await getBlockchainTransactions(blockNumber);
    console.log(transakcije);
}

getBlock is async, hence you are resolving an empty array because for loops in javascript are synchronous, but you're using an async callback inside them, and resolve is called before tx.push is. getBlock是异步的,因此你解决一个空数组,因为for在javascript是同步的循环,但你使用它们内部的异步回调,并resolve被调用之前tx.push是。

I would suggest you a recursive async approach, like this:我建议您采用递归异步方法,如下所示:

function getBlockchainTransactions(blockNumber){
    var tx = [];
    return new Promise(resolve => {
            // declare a recrusive async loop.
         var recursiveAsyncLoop = function(current, max) {
            // If the current index is exactly blockNumber, resolve.
          if (current === max) {
            resolve(tx);
          }
          // Otherwise, excute the operation on the actual block.
          else {
            var i = current;
            web3.eth.getBlock(i, function(error, block){
              if(!error && block.transactions.length != 0){
                console.log(block.transactions);
                tx.push(block.transactions);
                // once the operation is finished, increase the counter on the next call.
                recursiveAsyncLoop(current + 1, max);
              }
              // In cany case, regardless the above is true or false, continue.
              else recursiveAsyncLoop(current + 1, max);
            })
          }
         }
         // Begin the loop, from index 0 until blockNumber (excluded).
         recursiveAsyncLoop(0, blockNumber);
    });

}


async function msg() {
    const transakcije = await getBlockchainTransactions(blockNumber);
    console.log(transakcije);
}

The above code should call resolve only when the items are effectively added to the array.仅当项目有效地添加到数组时,上面的代码才应调用resolve

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM