Hate Names
Hate Names

Reputation: 1606

Array within an array, within an object?

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

Upvotes: 6

Views: 187

Answers (2)

Jake Lin
Jake Lin

Reputation: 11494

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

HeroName.Spells[0] = {};

Upvotes: 3

Paul
Paul

Reputation: 141829

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 = [];

Upvotes: 7

Related Questions