I want to update elements into an array from an $.each loop but it loads them incorrectly into the array. it puts all users into one element, all dates into one element and so on
Here is the code:
var array = [{
user: "",
user_id: "",
date: "",
profile_img: "",
text: "",
contentString: "",
url: "",
location: ""
}];
$.getJSON("http://search.twitter.com/search.json?q=%23euronews&rpp=5&include_entities=true&result_type=mixed&callback=?",
function (data) {
$.each(data.results, function (i, item) {
var user = item.from_user;
var user_id = item.from_user_id;
var date = item.created_at;
var profile_img = item.profile_image_url;
var text = item.text;
var contentString = text;
var url = (item.entities.urls.length > 0 ? item.entities.urls[0].url : '');
console.log(i);
array.user=user;
array.user_id=user_id;
array.date=date;
array.profile_img=profile_img;
array.text=text;
array.contentString=contentString;
array.url=url;
});
console.log(array);
});
what shall i do to fix this ?
You should not put the update code in that
for ... inloop. The “i” value is passed into your function. There’s no need for theforloop.So just get rid of that loop around the
array[i]assignments.Also, the assignments are wrong:
etc.