Klaus Hartl wrote:

> Yes, there is a direct method, just use a for loop:
>
> for (var key in json) {
>      console.log(key); // key
>      console.log(json[key]); // value
> }
>
> Maybe I didn't get the question right?

Hi Klaus,

Ok, Now I see why I asked the question.  Yes, I did try the above, but
I see in some code snippet I was struggling to grasp JSON Objects,
Strings and Arrays:

Here is a example JSON:

var json  = {
  fields:
  [
   {prompt1: "Login Name"},
   {prompt2: "Real Name"},
   {prompt3: "Location"},
   {prompt4: "Password"},
   {prompt5: "Security Group"},
   {prompt6: "File Area"}
  ]
};

Essentially, I wanted to pull the property names ("prompt1" to
"prompt6").  They don't need to have to this pattern in my case.  The
prompts can have varying names.

So when I used the "for in" syntax above, it wasn't quite what I
wanted.  For example:

   for (var i in json) {
       console.log(i, " | ",json[i]);
       for (var k in json[i]) {
          console.log(k," | ",json[i][k]);
       }
   }

Produces this:

fields  |  [Object prompt1=Login Name, Object prompt2=Real Name,
Object prompt3=Location, Object prompt4=Password, Object
prompt5=Security Group, Object prompt6=File Area]
0 | Object prompt1=Login Name
1 | Object prompt2=Real Name
2 | Object prompt3=Location
3 | Object prompt4=Password
4 | Object prompt5=Security Group
5 | Object prompt6=File Area

I figured it out know.  You got to look at the constructor type to see
if its an Object, Array or String.   From there you can decide to use
for each or for in or for loop.

I just figured that with all the "selector" stuff, that they would be
a more direct  way. :-)

--
HLS

Reply via email to