“for in” loop to print object properties with Javascript/Actionscript

Do you want to know object properties of any object? Its very easy listing all the properties and values with javascript/actionscript using “for in” looping structure.

Lets see how.

Syntax:

for(prop in object)
{

}

Above structure shows how to apply “for in” structure on object.

How it works?
-Its start collecting all properties name to the left side variable (prop) with in the bracket.

Eg.

<script language=”Javascript”>
var obj = new Object(); //Create new object
obj[“firstName”]=”vijay”; //Assign property firstName with value vijay
obj[“lastName”]=”khambalkar”;//Assign property lastName with value Khambalkar
var str=””; //variable which will hold property values
for(prop in obj)
{
str+=prop + ” value :”+ obj[prop]+”\n”;//Concate prop and its value from object
}
alert(str); //Show all properties and its value
</script>

I used this technique many times while working with actionscript and javascript. This all me to see all properties and its individual value to track.

Advertisements

13 Responses to “for in” loop to print object properties with Javascript/Actionscript

  1. Alexandr says:

    before name of scaned param of object u need write var like this

    for(var prop in object) {}

    not

    for (var prop in object) {}

    coz in IE without var don’t work

  2. jl says:

    Thanks! This is very helpful.

  3. lchild385 says:

    Great tip, thanks a lot.

  4. mmhh says:

    gfhgfhdgfhgfhdfg

  5. Thanks for these helpful tips. Also, thanks to Alexandr for the IE fix.

  6. Mat says:

    Very useful…it should has to exist a function to enumerate all properties

  7. AIDSTER says:

    very nice !!! thanks so much!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: