A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Iterate an Object

  1. #1
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813

    Angry Iterate an Object

    Now I know this seems like a very simple matter, in fact, I am pretty sure I've done this before, but during the current project I am running is a strange behavior.

    First the code:

    Code:
    private function GenerateParamString(obj:Object):String
    {
        var fullStr:String = "{";
    
        for (var key:String in obj)
        {
            fullStr += "'" + key + "':";
    
             if(obj[key] is String)
                fullStr += "'" + obj[key] + "', ";
            else
                fullStr += obj[key] + ", ";
        }
    
        fullStr += "}";
    
        return fullStr;
    }
    Okay, now what I looking to do is create a string variable that looks similar to this:
    {'Model':'SomeModelNumber', 'EID':1234, 'ItemID':5, 'Size':null, 'iLength':34, 'iWidth':30}

    When I execute the function, it makes it to the FOR statement, but then exits without iterating through each object. I am completely at a loss here as to why. There is nothing cryptic about the information, but not sure why it won't iterate through the object, pulling out the requested information.

    Does anyone have a clue as to why it is doing this?
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Your code works, so how are you creating your object?
    Code:
    var obj:Object = new Object();
    obj.Model = 'SomeModelNumber';
    obj.EID = 1234;
    obj.ItemID = 5;
    obj.Size = null;
    obj.iLength = 34;
    obj.iWidth = 30;
    
    trace(GenerateParamString(obj)); // {'Model':'SomeModelNumber', 'Size':null, 'EID':1234, 'iLength':34, 'ItemID':5, 'iWidth':30}
    
    function GenerateParamString(obj:Object):String {
            var fullStr:String = "{";
            for (var key:String in obj) {
                    fullStr +=  "'" + key + "':";
                    if (obj[key] is String) {
                            fullStr +=  "'" + obj[key] + "', ";
                    } else {
                            fullStr +=  obj[key] + ", ";
                    }
            }
    // remove last comma and space
            fullStr = fullStr.substr(0,fullStr.length - 2);
            fullStr +=  "}";
            return fullStr;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center