Snart Posted February 5, 2009 Share Posted February 5, 2009 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 Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 5, 2009 Share Posted February 5, 2009 you almost had it. "i" in your example is key for car, not the value. so it should be like this: <script> car = new Object(); car.type = "type"; car.brand = "brand"; for(var i in car) { alert(car[i]); } </script> Quote Link to comment Share on other sites More sharing options...
Snart Posted February 5, 2009 Author Share Posted February 5, 2009 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). Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 5, 2009 Share Posted February 5, 2009 the code you just posted worked for me.... you may also want to look into the Firebug plugin for Firefox. It will let you look at an object and it's properties without having to use JavaScript to alert it out Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.