我目前正在使用Javascript中的項目。我有一個我想要迭代的String對象數組。但是,我無法弄清楚如何訪問對象的每個屬性的Key。訪問Javascript中的對象數組中的鍵
例如:
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
---
如何訪問主題,發佈者等以迭代的方式?
剛打印變量名爲_key_(你自己聲明)... – jahroy
'(var key in jsonDoc [i]){console.log(key);}' – tymeJV