Jump to content

Cycling object properties


Snart

Recommended Posts

Hi

 

Is there a way to cycle through a javascript object's properties?  For example:

 

car.type = "";

car.brand = "";

 

for(var i in car)

{

  alert(i);

}

 

I simply need to know all possible properties of an object, not their contents.  The above doesn't seem to work, though.

 

Cheers

Link to comment
https://forums.phpfreaks.com/topic/143912-cycling-object-properties/
Share on other sites

Thanks, but that is just the issue.  I don't need the values, I need the property names.  Suppose I have a "car" object with only 3 possible properties:

 

car.type = "SUV"

car.brand = "Ford"

car.color = "Blue"

 

The thing is, I only know that I can use the "car" object.  I don't know which properties it supports.  So I need something like:

 

//Show me all properties

var x = "You can use: ";

for(var i in car)

{

  x = x + i + " ";

}

 

alert(x); //Outputs: "You can use type brand color". 

 

So now I know that car.licenseplate will not work.  "car" is not an object I made, it is part of an app I use and I need to know what properties I can use with it (no documentation available on the app).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.