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]
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]
- You're missing a ma between the array values in the data array. – Jared Commented Sep 17, 2008 at 19:50
- thanks a lot for the help..yes, I changed the names for the post and messed up the syntax, thanks again – Jay Corbett Commented Sep 17, 2008 at 20:40
9 Answers
Reset to default 4I 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]] );
}
First, you forgot your trailing mas in your data array items.
Try the following:
var obj_hash = {
"hdrs": ["Make", "Model", "Year"],
"data": [
{"Make": "Honda", "Model": "Accord", "Year": "2008"},
{"Make": "Toyota", "Model": "Corolla", "Year": "2008"},
{"Make": "Honda", "Model": "Pilot", "Year": "2008"},
]
};
var ref_data = obj_hash.data;
alert(ref_data[0].Make);
@Kent Fredric: note that the last ma is not strictly needed, but allows you to more easily move lines around (i.e., if you move or add after the last line, and it didn't have a ma, you'd have to specifically remember to add one). I think it's best to always have trailing mas.
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...
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 :)
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"
perhaps try data[0].Make
Close, you'd use
var x = data[0].Make;
var z = data[0].Model;
var y = data[0].Year;
Your code as displayed is not syntactically correct; it needs some mas. 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"]
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 += " " ; }
else { myTable += t.data[i][t.hdrs[j]] ; }
myTable += "</td>";
}
myTable += "</tr>";
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742411425a4438890.html
评论列表(0条)