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
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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.