I'm currently working on a function to iterate through an array of objects retrieved from the server. When I console.log
the response, this is the structure I see.
https://i.sstatic.net/ErOps.png
This is the JavaScript function I've created for this task—
var createPosts = ((data) => {
var postsArrayLength = data.response.total_posts;
for ( i = 0; i < postsArrayLength; i++ ) {
//for each post create a div
var postDiv = document.createElement('div');
postDiv.className = 'post ' + data.response.posts.postsArrayLength[i].type;
}
});
But now, I'm getting this error message—
Uncaught TypeError: Cannot read property '0' of undefined
This issue seems to occur only when trying to access an object with an integer as its name. Any suggestions on how to handle this or am I approaching it incorrectly? Thank you!