An array inside an array inside an object?

HeroName = new Hero() HeroName.Spells = []; HeroName.Spells[0].Type = []; 

This does not work = (even if I try a new Array () or anything else. Can't make arrays inside arrays? This is what I was going to do:

 HeroName.Spells[0].Type[0] = new DmgSpell(); HeroName.Spells[0].Type[1] = new Buff(); 

I know I can do something like

 HeroName.Spells[0][0] = new DmgSpelL(); HeroName.Spells[0][1] = new Buff(); 

But it's not so easy to read

Am I doing something wrong? I tried all possible combinations that I could think of, and using Google to search for an array in an array gives me other results that don't help me. Any help is appreciated

+6
source share
2 answers

You have skipped a step. You did not declare HeroName.Spells[0] object, so there cannot be a Type property for it because it does not exist. It works:

 HeroName = new Hero(); HeroName.Spells = []; HeroName.Spells[0] = {}; HeroName.Spells[0].Type = []; 
+7
source

Set HeroName.Spells [0] as an object, otherwise undefined . undefined cannot have any properties.

 HeroName.Spells[0] = {}; 
+3
source

Source: https://habr.com/ru/post/952703/


All Articles