Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 12144
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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