By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,029 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,029 IT Pros & Developers. It's quick & easy.

Object Property Looping

helimeef
P: 77
Is there any way for me to loop through the properties of an object as if it were an array? Like say I have an object like this...
Expand|Select|Wrap|Line Numbers
  1. {
  2.   a:'value 1';
  3.   b:'value 2';
  4.   c:'value 3';
  5. }
  6.  
... could I create a script that could retrieve the names and values of each property without knowing what the property names are?
What I'm doing this for is I'm trying to see if I can copy jQuery's CSS function (just for fun, not stealing)...
Thanks in advanced!
Nov 15 '07 #1
Share this Question
Share on Google+
2 Replies


Dasty
Expert 100+
P: 101
Expand|Select|Wrap|Line Numbers
  1. var obj = {a:'value 1', b:'value 2', c:'value 3'};
  2.  
  3. for (index in obj)
  4. {
  5.   alert(index + ':' + obj[index]);
  6. }
Nov 15 '07 #2

helimeef
P: 77
Expand|Select|Wrap|Line Numbers
  1. var obj = {a:'value 1', b:'value 2', c:'value 3'};
  2.  
  3. for (index in obj)
  4. {
  5.   alert(index + ':' + obj[index]);
  6. }
Thanks! I got it working!
The sad part is that I knew you could do that, however I thought it only worked for array keys. Well I learn a new thing every day :)
Nov 16 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.