jQuery의 parseJSON 함수를 사용하여 구문 분석하는 JSON 객체가 있습니다. 어떤 이유고정 길이 루프에서 Javascript 객체가 정의되지 않음
var posts = feedObj["posts"];
content+= "<h2 title=\"" + feedDescription + "\"><a href=\"" + feedPermalink + "\">" + feedTitle + "</a></h2>";
content+= "<ul class=\"feedList\">";
for (var i = 0; i < 10; i++) {
console.log(posts[i]["postTitle"]);
var postTitle = posts[i]["postTitle"];
if((typeof posts[i] != "undefined") || postTitle != null) {
content+= "<li>";
console.log("AJ::PostTitle"+postTitle);
content+= "<a href=\"" + decodeURIComponent(posts[i]["permaLink"]) + "\" target=\"_blank\">" + unescapeHTML(postTitle) + "</a>";
content+= "</li>";
}
}
의 var postTitle = posts[i]["postTitle"];
항상 다음과 같은 오류 제공 : 내가 가지고있는 Uncaught TypeError: Cannot read property 'postTitle' of undefined
을 나는 아이 중 하나가 다음과 같이 그때 루프를 사용하여 반복 로컬 변수에 객체를 할당 할 왜 이런 일이 일어나는 지 알지 못합니다. 콘솔 명령문은 postTitle을 올바르게 출력하지만 할당은 항상 실패합니다. 내가 도대체 뭘 잘못하고있는 겁니까? 대신
for (var i = 0; i < 10; i++) {
의
으로, 처음 10 개 게시물을 얻을 바꾸려면,'당신의 기존 전에 로그 문? 'posts' 배열의 인덱스 중 하나가 정의되지 않은 것처럼 보입니다. 대신 로그 문에서 오류가 발생했을 수 있습니다. – Krease
@Chris 그게 문제였습니다. for 루프 조건을 posts.length로 변경하여 수정했습니다. –