I have data that I get from an API in another function. To get the data I do this:
var posts = get_posts();
I can then print to the console:
console.info( posts );
I'll end up with something like this:
[]
0: {id: 123, date: "2018-01-01T12:00:00", slug: "asdf-xyz", author: 1, media: 1, …}
1: {id: 123, date: "2018-01-01T12:00:00", slug: "asdf-xyz", author: 1, media: 1, …}
...
There's multiple entries in this array with various bits of data.
However, if I then try to loop through them and do something, the each statement doesn't do anything. It's as if it's completely ignored (which it probably is).
$.each( posts, function( index, record ) {
alert( index );
});
What am I doing wrong here?
Edit
This is the code I have inside a function which returns (or should) return the array. Using a simple console.log
returns the data, however using stringify
just returns []
.
$.each( object, function( key, value ) {
var post = {
id: value.id,
...
};
posts.push( post );
});
console.log( posts ); // prints the data out
JavaScript is not my strongest coding language so I'm assuming I'm setting up the data incorrectly perhaps?