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












-1















I've got array which has another array which contains data i need.



How do i access "0x88def628c16651eb0d86be5ead3d738d0cb27fe947bb786c23105ac5d67a1bd0" in javascript for example? This is being displayed by calling var transakcije.



I've tried with:



transakcije[0][0] but that is not the name of sub array,



transakcije[0],



for loop (transakcija as transakcije) to no avail.



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().





enter image description here





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);
}









share|improve this question




















  • 3





    transakcije[0][0] should work

    – Shubham Khatri
    Nov 14 '18 at 13:11






  • 1





    transakcije[0][0] is correct, though the little "i" next to the log suggests me that it was immediately evaluated, hence I'm guessing that it may be the result of an asynchronous operation... is it?

    – briosheje
    Nov 14 '18 at 13:11











  • Yes, this is asynchronous operation. I've tried again with transakcije[0][0] and i get error: "Uncaught (in promise) TypeError: Cannot read property '0' of undefined"

    – energetics
    Nov 14 '18 at 13:17











  • Show more code context. There isn't enough detail in question as to where in that asynchronous code you try to access the array. What you see in console is likely being populated after you are logging it

    – charlietfl
    Nov 14 '18 at 13:22













  • web3.eth.getBlock is async, hence you are calling resolve before actually adding the elements to the array. You should make an async for, or something like that.

    – briosheje
    Nov 14 '18 at 13:33
















-1















I've got array which has another array which contains data i need.



How do i access "0x88def628c16651eb0d86be5ead3d738d0cb27fe947bb786c23105ac5d67a1bd0" in javascript for example? This is being displayed by calling var transakcije.



I've tried with:



transakcije[0][0] but that is not the name of sub array,



transakcije[0],



for loop (transakcija as transakcije) to no avail.



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().





enter image description here





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);
}









share|improve this question




















  • 3





    transakcije[0][0] should work

    – Shubham Khatri
    Nov 14 '18 at 13:11






  • 1





    transakcije[0][0] is correct, though the little "i" next to the log suggests me that it was immediately evaluated, hence I'm guessing that it may be the result of an asynchronous operation... is it?

    – briosheje
    Nov 14 '18 at 13:11











  • Yes, this is asynchronous operation. I've tried again with transakcije[0][0] and i get error: "Uncaught (in promise) TypeError: Cannot read property '0' of undefined"

    – energetics
    Nov 14 '18 at 13:17











  • Show more code context. There isn't enough detail in question as to where in that asynchronous code you try to access the array. What you see in console is likely being populated after you are logging it

    – charlietfl
    Nov 14 '18 at 13:22













  • web3.eth.getBlock is async, hence you are calling resolve before actually adding the elements to the array. You should make an async for, or something like that.

    – briosheje
    Nov 14 '18 at 13:33














-1












-1








-1


1






I've got array which has another array which contains data i need.



How do i access "0x88def628c16651eb0d86be5ead3d738d0cb27fe947bb786c23105ac5d67a1bd0" in javascript for example? This is being displayed by calling var transakcije.



I've tried with:



transakcije[0][0] but that is not the name of sub array,



transakcije[0],



for loop (transakcija as transakcije) to no avail.



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().





enter image description here





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);
}









share|improve this question
















I've got array which has another array which contains data i need.



How do i access "0x88def628c16651eb0d86be5ead3d738d0cb27fe947bb786c23105ac5d67a1bd0" in javascript for example? This is being displayed by calling var transakcije.



I've tried with:



transakcije[0][0] but that is not the name of sub array,



transakcije[0],



for loop (transakcija as transakcije) to no avail.



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().





enter image description here





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);
}






javascript arrays nested






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 9:41







energetics

















asked Nov 14 '18 at 13:09









energeticsenergetics

154




154








  • 3





    transakcije[0][0] should work

    – Shubham Khatri
    Nov 14 '18 at 13:11






  • 1





    transakcije[0][0] is correct, though the little "i" next to the log suggests me that it was immediately evaluated, hence I'm guessing that it may be the result of an asynchronous operation... is it?

    – briosheje
    Nov 14 '18 at 13:11











  • Yes, this is asynchronous operation. I've tried again with transakcije[0][0] and i get error: "Uncaught (in promise) TypeError: Cannot read property '0' of undefined"

    – energetics
    Nov 14 '18 at 13:17











  • Show more code context. There isn't enough detail in question as to where in that asynchronous code you try to access the array. What you see in console is likely being populated after you are logging it

    – charlietfl
    Nov 14 '18 at 13:22













  • web3.eth.getBlock is async, hence you are calling resolve before actually adding the elements to the array. You should make an async for, or something like that.

    – briosheje
    Nov 14 '18 at 13:33














  • 3





    transakcije[0][0] should work

    – Shubham Khatri
    Nov 14 '18 at 13:11






  • 1





    transakcije[0][0] is correct, though the little "i" next to the log suggests me that it was immediately evaluated, hence I'm guessing that it may be the result of an asynchronous operation... is it?

    – briosheje
    Nov 14 '18 at 13:11











  • Yes, this is asynchronous operation. I've tried again with transakcije[0][0] and i get error: "Uncaught (in promise) TypeError: Cannot read property '0' of undefined"

    – energetics
    Nov 14 '18 at 13:17











  • Show more code context. There isn't enough detail in question as to where in that asynchronous code you try to access the array. What you see in console is likely being populated after you are logging it

    – charlietfl
    Nov 14 '18 at 13:22













  • web3.eth.getBlock is async, hence you are calling resolve before actually adding the elements to the array. You should make an async for, or something like that.

    – briosheje
    Nov 14 '18 at 13:33








3




3





transakcije[0][0] should work

– Shubham Khatri
Nov 14 '18 at 13:11





transakcije[0][0] should work

– Shubham Khatri
Nov 14 '18 at 13:11




1




1





transakcije[0][0] is correct, though the little "i" next to the log suggests me that it was immediately evaluated, hence I'm guessing that it may be the result of an asynchronous operation... is it?

– briosheje
Nov 14 '18 at 13:11





transakcije[0][0] is correct, though the little "i" next to the log suggests me that it was immediately evaluated, hence I'm guessing that it may be the result of an asynchronous operation... is it?

– briosheje
Nov 14 '18 at 13:11













Yes, this is asynchronous operation. I've tried again with transakcije[0][0] and i get error: "Uncaught (in promise) TypeError: Cannot read property '0' of undefined"

– energetics
Nov 14 '18 at 13:17





Yes, this is asynchronous operation. I've tried again with transakcije[0][0] and i get error: "Uncaught (in promise) TypeError: Cannot read property '0' of undefined"

– energetics
Nov 14 '18 at 13:17













Show more code context. There isn't enough detail in question as to where in that asynchronous code you try to access the array. What you see in console is likely being populated after you are logging it

– charlietfl
Nov 14 '18 at 13:22







Show more code context. There isn't enough detail in question as to where in that asynchronous code you try to access the array. What you see in console is likely being populated after you are logging it

– charlietfl
Nov 14 '18 at 13:22















web3.eth.getBlock is async, hence you are calling resolve before actually adding the elements to the array. You should make an async for, or something like that.

– briosheje
Nov 14 '18 at 13:33





web3.eth.getBlock is async, hence you are calling resolve before actually adding the elements to the array. You should make an async for, or something like that.

– briosheje
Nov 14 '18 at 13:33












1 Answer
1






active

oldest

votes


















1














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.



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.






share|improve this answer
























  • It works now, it can be accessed with double arrays like transakcije[216][0].

    – energetics
    Nov 15 '18 at 9:43











  • @energetics you may customize it by removing useless code. For instance, you may refactor by calling recursiveAsyncLoop in the else only once, just take the above as an example, it can be improved ;)

    – briosheje
    Nov 15 '18 at 11:27











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301012%2fhow-to-access-data-inside-double-nested-array-in-async-function-javascript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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.



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.






share|improve this answer
























  • It works now, it can be accessed with double arrays like transakcije[216][0].

    – energetics
    Nov 15 '18 at 9:43











  • @energetics you may customize it by removing useless code. For instance, you may refactor by calling recursiveAsyncLoop in the else only once, just take the above as an example, it can be improved ;)

    – briosheje
    Nov 15 '18 at 11:27
















1














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.



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.






share|improve this answer
























  • It works now, it can be accessed with double arrays like transakcije[216][0].

    – energetics
    Nov 15 '18 at 9:43











  • @energetics you may customize it by removing useless code. For instance, you may refactor by calling recursiveAsyncLoop in the else only once, just take the above as an example, it can be improved ;)

    – briosheje
    Nov 15 '18 at 11:27














1












1








1







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.



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.






share|improve this answer













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.



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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 13:39









brioshejebriosheje

3,24112138




3,24112138













  • It works now, it can be accessed with double arrays like transakcije[216][0].

    – energetics
    Nov 15 '18 at 9:43











  • @energetics you may customize it by removing useless code. For instance, you may refactor by calling recursiveAsyncLoop in the else only once, just take the above as an example, it can be improved ;)

    – briosheje
    Nov 15 '18 at 11:27



















  • It works now, it can be accessed with double arrays like transakcije[216][0].

    – energetics
    Nov 15 '18 at 9:43











  • @energetics you may customize it by removing useless code. For instance, you may refactor by calling recursiveAsyncLoop in the else only once, just take the above as an example, it can be improved ;)

    – briosheje
    Nov 15 '18 at 11:27

















It works now, it can be accessed with double arrays like transakcije[216][0].

– energetics
Nov 15 '18 at 9:43





It works now, it can be accessed with double arrays like transakcije[216][0].

– energetics
Nov 15 '18 at 9:43













@energetics you may customize it by removing useless code. For instance, you may refactor by calling recursiveAsyncLoop in the else only once, just take the above as an example, it can be improved ;)

– briosheje
Nov 15 '18 at 11:27





@energetics you may customize it by removing useless code. For instance, you may refactor by calling recursiveAsyncLoop in the else only once, just take the above as an example, it can be improved ;)

– briosheje
Nov 15 '18 at 11:27




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301012%2fhow-to-access-data-inside-double-nested-array-in-async-function-javascript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python