views:

558

answers:

9

If I have the following:

{"hdrs": ["Make","Model","Year"],
 "data" : [ 
   {"Make":"Honda","Model":"Accord","Year":"2008"}
   {"Make":"Toyota","Model":"Corolla","Year":"2008"}
   {"Make":"Honda","Model":"Pilot","Year":"2008"}]
}

And I have a "hdrs" name (i.e. "Make"), how can I reference the "data" array instances? seems like data["Make"][0] should work...but unable to get the right reference

EDIT Sorry for the ambiguity..I can loop through "hdrs" to get each hdr name, but I need to use each instance value of "hdrs" to find all the data elements in "data" (not sure that is any better of an explanation). and I will have it in a variable "t" since it is JSON (appreciate the re-tagging) I would like to be able to reference with something like this: t.data[hdrs[i]][j]

A: 

perhaps try data[0].Make

aweber1
A: 

Close, you'd use

var x = data[0].Make;
var z = data[0].Model;
var y = data[0].Year;
Stephen Wrighton
+1  A: 

So, like this?

var theMap = /* the stuff you posted */;
var someHdr = "Make";
var whichIndex = 0;
var correspondingData = theMap["data"][whichIndex][someHdr];

That should work, if I'm understanding you correctly...

Domenic
+2  A: 

I had to alter your code a little:

var x = {"hdrs": ["Make","Model","Year"],
   "data" : [ 
     {"Make":"Honda","Model":"Accord","Year":"2008"},
     {"Make":"Toyota","Model":"Corolla","Year":"2008"},
     {"Make":"Honda","Model":"Pilot","Year":"2008"}]
  };

  alert( x.data[0].Make );

EDIT: in response to your edit

var x = {"hdrs": ["Make","Model","Year"],
   "data" : [ 
     {"Make":"Honda","Model":"Accord","Year":"2008"},
     {"Make":"Toyota","Model":"Corolla","Year":"2008"},
     {"Make":"Honda","Model":"Pilot","Year":"2008"}]
  };
var Header = 0; // Make
for( var i = 0; i <= x.data.length - 1; i++ )
{
    alert( x.data[i][x.hdrs[Header]] );
}
Jared
+1  A: 
var x = {"hdrs": ["Make","Model","Year"],
 "data" : [ 
   {"Make":"Honda","Model":"Accord","Year":"2008"}
   {"Make":"Toyota","Model":"Corolla","Year":"2008"}
   {"Make":"Honda","Model":"Pilot","Year":"2008"}]
};

x.data[0].Make == "Honda"
x['data'][0]['Make']  == "Honda"

You have your array/hash lookup backwards :)

Kent Fredric
A: 

Your code as displayed is not syntactically correct; it needs some commas. I got this to work:

$foo = {"hdrs": ["Make","Model","Year"],
 "data" : [ 
   {"Make":"Honda","Model":"Accord","Year":"2008"},
   {"Make":"Toyota","Model":"Corolla","Year":"2008"},
   {"Make":"Honda","Model":"Pilot","Year":"2008"}]
};

and then I can access data as:

$foo["data"][0]["make"]
DGM
thanks for the response, you are right about the commas, I changed the names for the post and messed up the syntax
CarolinaJay65
+1  A: 
just mike
[{},{},{},] <-- last comma here *not* needed.
Kent Fredric
+1  A: 

I'm not sure I understand your question, but...

Assuming the above JSON is the var obj, you want:

obj.data[0]["Make"] // == "Honda"

If you just want to refer to the field referenced by the first header, it would be something like:

obj.data[0][obj.hdrs[0]] // == "Honda"
noah
A: 

With the help of the answers (and after getting the inside and outside loops correct) I got this to work:

var t = eval( "(" + request + ")" ) ;
for (var i = 0; i < t.data.length; i++) {
 myTable +=    "<tr>";
 for (var j = 0; j < t.hdrs.length; j++) {
  myTable += "<td>" ;
   if (t.data[i][t.hdrs[j]] == "") {myTable += "&nbsp;" ; }
    else { myTable += t.data[i][t.hdrs[j]] ; }
  myTable += "</td>";
 }
 myTable +=    "</tr>";
}
CarolinaJay65