• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 12152
  • Last Modified:

Loop through each item of an object in javascript

I was wondering if it was possible to loop through each item of an object in javascript.

I have tried something similar to
----------------------------------------------------------------------
for (i in thisRowProperties)alert(instance[i]);
----------------------------------------------------------------------
but that doesnt work.

My object is as follows (returned through JSON - the elements within the object are variable and I dont want to have to hard code references to the items within this object):
----------------------------------------------------------------------
 "someObject": {
                    "candothis1": false,
                    "candothis2": true,
                    "candothis3": true,
                    "candothis4": true,
                    "candothis5": true,
                }
----------------------------------------------------------------------
which would give me the ability to reference someObject.candothis1 and get its value.

Can I loop through that object anyway and get a list  of items and their values without hardcoding my references?
0
greenskwerl
Asked:
greenskwerl
  • 3
  • 2
  • 2
  • +1
1 Solution
 
ZylochCommented:
The for loop is the right way to go. What is your Javascript/JSON setup look like in code? For example, something like this:

var obj = {
    item1: false,
    item2: true
};

can be looped over by using

for (prop in obj) {
    alert(obj[prop]);
}
0
 
basicinstinctCommented:
<html>
      <head>
      <script>
            function test()
      {
            var o = new Object();
            o.prop = "holden";
            o.prop_one = "caufield";
            for(i in o)
            {
                  alert(i + " = " + o[i]);
                  
            }
      }
      </script>
      </head>
      <body onload="test();">
      </body>
</html>
0
 
hieloCommented:
First you need to "convert" it to an object:
var json=' "someObject": {
                    "candothis1": false,
                    "candothis2": true,
                    "candothis3": true,
                    "candothis4": true,
                    "candothis5": true,
                }';
var eval("var x={"+json+"};");

//now loop
for( var i in x.someObject)
 for( var j in i)
  alert(x.someObject[j]);
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
hieloCommented:
Update: There should be no var infront of eval
0
 
ZylochCommented:
That's right. If you just have a JSON string, not an object, you will need to use eval(), like hielo says, or use something like parseJSON() from http://www.json.org/js.html which will sanitize your JSON string.
0
 
greenskwerlAuthor Commented:
My JSON data was already evaluated into an object.  


This does work.  

for (prop in obj) {
    alert(obj[prop]);
}

Is there a way to find out what each item is that I am alerting, ie. show what each prop is called in the object?

It would be great if I could alert both the value of prop and its name.
0
 
ZylochCommented:
You would just do this:

for (prop in obj) {
    alert(prop + ": " + obj[prop]);
}

You can also store this information in a variable and build up a string to alert all at once.
0
 
greenskwerlAuthor Commented:
Nevermind, its simply

for (prop in obj) {
    alert(prop);
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now