0

maybe I'm forgetting something, but I can not loop over and array inside an object.

I have this:

    var myCategory = {
        id: snapshot.id,
        name: category.name,
        order: category.order,
        status: category.status,
        menuOptions:[]
    }

Then I push options

myCategory.menuOptions.push(menuOption);

then I log myCategory and get this:

{id: 'K1VXbeGo3qivFAV61x5P2', name: 'Combos', order: 1, status: 'A', menuOptions: Array(0)}
id: "K1VXbeGo3qivFAV61x5P2"
menuOptions: Array(2)
0: {id: 'LFtAWpu9iKUY0fjXvuji', categoryId: 'K1VXbeGo3qivFAV61x5P2', name: 'Escoge tu salsa', order: 1, OptionsOnItemInCart: undefined, …}
1: {id: 'zp5AwxU04XhhVcGjep1F', categoryId: 'K1VXbeGo3qivFAV61x5P2', name: 'Adicionales', order: 1, OptionsOnItemInCart: undefined, …}
length: 2
[[Prototype]]: Array(0)
name: "Combos"
order: 1
status: "A"
[[Prototype]]: Object

Now I try to loop over myCategory.menuOptions but I can not, this: Array.isArray( category.menuOptions) give me a true,

but when I call category.menuOptions.length return 0.

Any idea? thanks.

JhonArias
  • 127
  • 1
  • 3
  • 11
  • 1
    Can you update the question to include a runnable code snippet which demonstrates the problem? – David Nov 05 '21 at 12:43
  • 1
    I think I know what this is: you're pushing options into the array asynchronously, but trying to access its length without waiting for that async function to complete, aren't you? – Niet the Dark Absol Nov 05 '21 at 12:44
  • @NiettheDarkAbsol yes; I'm pushing the options in aasync func I use await to get the MyCategory and then try to loop options. – JhonArias Nov 05 '21 at 12:52

0 Answers0