I have a JSON object that I parse using jQuery’s parseJSON function. I assign one of the child objects to a local variable which I then iterate over using a for loop as follows:
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>";
}
}
For some reason, the var postTitle = posts[i]["postTitle"]; always gives the following error:
Uncaught TypeError: Cannot read property 'postTitle' of undefined
I have no idea why this is happening. The console statement prints out the postTitle correctly but the assignment always fails. What am I doing wrong?
Instead of
Add the check condition for the array object
Maybe the array you are referring to might not have 10 entries in it. The later approach should solve the problem..