我目前正在开发一个 Javascript 项目。我有一个要迭代的 String 对象数组。但是,我无法弄清楚如何访问对象每个属性的 Key。
例如:
var jsonDoc = [
{
"Subject": "XXXXXXX",
"Submitter": "X",
"Dx": "Affected",
"Sample Set": "Arab",
"Sex": "F",
"Pedigree": "0"
},
{
"Subject": "XXXXXXX",
"Submitter": "X",
"Dx": "Affected",
"Sample Set": "North American",
"Sex": "F",
"Pedigree": "0"
}
]
for( var i = 0; i<jsonDoc.length; i++){
for(var key in jsonDoc[i]){
document.write(jsonDoc[i][key]+"<br />");
}
document.write("--- <br />");
}
但是,这只会打印值而不是键:
XXXXXXX
X
Affected
Arab
F
0
---
XXXXXXX
X
Affected
North American
F
0
---
如何以迭代方式访问主题、提交者等?