I have a jQuery.each loop iterating through a JSON object that has three sets of data, but for some reason the loop outputs 13 objects instead of 3, and 10 of them are empty. Here is my code:
var teammates_info=[];
var teammate_json={"teammates":[{"id":"12","first_name":"Bob","last_name":"Johnson","user_img":"","status":"offline","new_messages":"0"},{"id":"9","first_name":"John","last_name":"Doe","user_img":"","status":"offline","new_messages":"0"},{"id":"10","first_name":"test","last_name":"test","user_img":"","status":"offline","new_messages":"0"}]};
jQuery.each(teammate_json.teammates,function(index,teammate){
teammates_info[teammate.id]=[{"first_name":teammate.first_name,"last_name":teammate.last_name,"user_img":teammate.user_img,"status":teammate.status,"new_messages":teammate.new_messages}];
});
console.log(teammates_info.length); //outputs 13 not 3
If I do a document.write(teammates_info);
I get ,,,,,,,,,[object Object],[object Object],,[object Object]
, so you can see all the empty array elements that are being created.
I am trying to output an array of objects, that way I can reference a teammate's data later in my code like teammates_info[12]["first_name"]
, but I am stumped as to why 13 array elements are created instead of just 3.