Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7871993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:04:41+00:00 2026-06-03T02:04:41+00:00

I may be doing something really stupid, but I don’t get why the below

  • 0

I may be doing something really stupid, but I don’t get why the below code doesn’t work…

Here, I create a generic Object called spellbook:

// A list of all the player's spells
public var spellBook:Object = {};

Here, I add a key-value pair to the spellbook:

spellBook["bubble"] = new BubbleSpell(spellBook);

And here I try to output the contents of the spellbook:

trace("Spells initialised.  Available spells are:");
for each (var _spell:String in spellBook)
{
    trace("  ", _spell, " : ", spellBook[_spell]);
}

But this is the output I get:

Spells initialised.  Available spells are:
   [object BubbleSpell]  :  undefined

What I don’t get is why it’s not outputting:

Spells initialised.  Available spells are:
   bubble : [object BubbleSpell]

??

It’s as if I’m iterating over the values in spellbook, rather than the keys… is that what’s happening? All the docs I’ve seen so far seem to indicate that this is the correct way to iterate over a dictionary (which a generic object kind of is…) Do I need to call a method to get keys instead of values for generic objects?

So confused!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:04:42+00:00Added an answer on June 3, 2026 at 2:04 am

    for each is used to iterate over values, you want to use a for loop which iterates over keys, eg:

    var mySpells = { 
        speedy: new SpeedySpell(),
        sleepy: new SleepySpell()
    };
    
    for (var key : String in mySpells) {
        trace("key: " + key + " value: " + mySpells[key]);
    }
    

    Note that when iterating over the keys in a Dictionary you should leave the key value untyped (*) as a Dictionary’s keys can be of any type (whereas an Object‘s keys can only be of type String).

    A common trick would be to create a utility function which extracts an Object’s keys to an Array for easier iteration:

    function getKeys(source : Object) : Array {
        const result : Array = [];
        for (var key : * in source) {
            result.push(key);
        }
        return result;
    }
    

    You can then make use of this helper function:

    trace(getKeys(mySpells));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I may be doing something really stupid here as I've done it before and
I may be missing something really simple here, but I am trying to style
I may be doing something wrong, but I haven't been able to find a
I may just be doing something wrong but all of my javascript files as
I'm not a PHP developer so I may be doing something wrong. I'm trying
May be a stupid questions but can I protect a row of data in
What I'm asking may sound strange, but I really need it.. What I need
Ok this may be really simple but everything I try just seems to hit
this is really stupid, but I been trying dozens of different things and I
i'm not sure if i'm doing something wrong in my code or if my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.