Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 07-23-2012, 09:44 PM   #1
tnel
Registered User
 
Join Date: Jun 2012
Posts: 35
Default for..in loop Question

Say I had an array that contained several associative arrays. Each associative array has 10 properties, of which 8 are objects, 1 is a string, and 1 is an integer. I would like to go through each associative array and access only the 8 objects. I have code like the following:

Code:
for(var i:int = 0; i < array.length; i++)
{
      for(var part:Object in array[i])
      {
            trace(part);
      }
}
I was expecting this to go through my associative arrays and only for those properties that are objects would it print the name of the property as a trace statement. However, all properties print out. How can I limit my results to only the properties that are objects?

Thank you.
tnel is offline   Reply With Quote
Old 07-23-2012, 10:19 PM   #2
[afz]snickelfitz
Senior Member
 
[afz]snickelfitz's Avatar
 
Join Date: Dec 2011
Location: Tucson, AZ
Posts: 2,331
Default

ActionScript Code:
var array:Array = ["string",1,2,{a:3,b:4},{a:5,b:6},{a:7,b:8}]; for each (var a in array) {     if ( !(a is String || a is int) )     {         trace(a);     } }
[afz]snickelfitz is offline   Reply With Quote
Old 07-23-2012, 10:35 PM   #3
tnel
Registered User
 
Join Date: Jun 2012
Posts: 35
Default Thanks but...

Thank you for the reply but that didn't work. I got the following error:

Code:
ReferenceError: Error #1069: Property wip not found on Number and there is no default value.
tnel is offline   Reply With Quote
Old 07-23-2012, 10:55 PM   #4
[afz]snickelfitz
Senior Member
 
[afz]snickelfitz's Avatar
 
Join Date: Dec 2011
Location: Tucson, AZ
Posts: 2,331
Default

You probably have a Number (such as .5) in the array. The code is only excluding integers and Strings. just replace int with Number within the if statement. Stuff like this is often trial and error.
ActionScript Code:
var array:Array = ["string",1, .5, {a:3,b:4},{a:5,b:6},{a:7,b:8}]; for each (var obj in array) {     if ( !(obj is String || obj is int || obj is Number) )     {         trace(obj.a, obj.b)     } }
BTW, if you run the code I posted in a new fla. it will work.
[afz]snickelfitz is offline   Reply With Quote
Old 07-23-2012, 11:44 PM   #5
Sonny
Senior Member
 
Join Date: Aug 2010
Location: Sydney,Australia
Posts: 132
Default

this is a very generalized object printing loop. changing the code a little bit you can get it to work the way you want

ActionScript Code:
function print_a(obj: Object, indent: String , isError:Boolean){     if (indent == null) indent = "";     var out = "";     for ( var item in obj ) {         if (obj[item] is Object && obj[item]== null ){             out += (isError?"[ERROR]":"") + indent+"[" + item + "] => Object  " + " \n";         }else{             out += indent+"[" + item + "] => " + obj[item]+"\n";         }         out += print_a( obj[item], indent+"   " ,isError);     }     return out; }
Sonny is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 12:00 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.