JSON 개체 (내가 정의했다고 생각한 것임)가 있고 그 안에있는 배열의 값에 액세스하려고합니다. 정확하지만 값 img.iName
은 항상 세 번 반복됩니다.undefined
내가 무엇을 오해 했습니까?
<div id="dbgDIV">Debug Div<br></div>
<script>
// imgs as a JSON array
var gallery = {"imgs":
[ // Height and Width to be added
{"iName":"File1.jpg", "tName": "File1_th.jpg","cap":"This is a Caption for File1"},
{"iName":"File2.jpg", "tName": "File2_th.jpg","cap":"This is a Caption for File2"},
{"iName":"File3.jpg", "tName": "File3_th.jpg","cap":"This is a Caption for File3"}
],
"imgCount":"3"
};
var dbgDIV = document.getElementById("dbgDIV");
for (var img in gallery.imgs) {
dbgDIV.innerHTML = dbgDIV.innerHTML + "img=" + img.iName + "<br>";
console.log(img.iName);
}
</script>
for...in
루프는 문제입니다. 전통적인 for
루프를 사용 하여 배열에 색인을 생성하십시오.
var gallery = {
"imgs": [
{
"iName": "File1.jpg",
"tName": "File1_th.jpg",
"cap": "This is a Caption for File1"
},
{
"iName": "File2.jpg",
"tName": "File2_th.jpg",
"cap": "This is a Caption for File2"
},
{
"iName": "File3.jpg",
"tName": "File3_th.jpg",
"cap": "This is a Caption for File3"
}
],
"imgCount": "3"
};
var dbgDIV = document.getElementById("dbgDIV");
for (var i = 0; i < gallery.imgs.length; i++) {
var img = gallery.imgs[i];
console.log(img.iName);
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다