Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array within an array, within an object?

Tags:

javascript

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

This doesnt work =( , even if I try new Array() or anything else. Is it not possible to do arrays within arrays? This is what I was going for:

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 this doesn't make it as easy to read

Am I doing something wrong? I've tried every possible combination I could think of and using google to search 'array within an array' gives me other results that don't help me. Any help is greatly appreciated

like image 799
Hate Names Avatar asked Aug 29 '13 01:08

Hate Names


2 Answers

You missed a step. You haven't declared HeroName.Spells[0] to be an object, so you can't a Type property to it, because it doesn't exist. This works:

HeroName = new Hero();
HeroName.Spells = [];
HeroName.Spells[0] = {};
HeroName.Spells[0].Type = [];
like image 197
Paul Avatar answered Oct 16 '22 01:10

Paul


Set HeroName.Spells[0] as an Object, otherwise, it is undefined. undefined can't have any properties.

HeroName.Spells[0] = {};
like image 40
Jake Lin Avatar answered Oct 15 '22 23:10

Jake Lin