Why Am I Only Getting The Last Item From Javascript For Loop?
Solution 1:
Yes your issue has to do with closures. Your get requests are likely finishing (and calling their respective callbacks) long after all of the requests have been sent. Because of this, by the time the callbacks are called, i
will be equal to lexicon.length
. You can fix this by enclosing the get request in its own functional scope and passing the index i
at that moment into that scope. Like this:
for (var i = 0; i < lexicon.length; i++) {
(function(i) {
$.getJSON('http://api.urbandictionary.com/v0/define?term=' + lexicon[i],
function(data){
lillyLivered = [];
lillyLivered.push(new word(data.list[0].word, data.list[0].definition, data.list[0].example));
console.log(lillyLivered);
$('.container-fluid').html('<div class="row message unread">' +
' <div class="col-xs-12 col-sm-6 col-lg-3 malok ' + color[i] + ' id="">' +
'<div class="row dict">' +
'<div class="col-xs-9 col-sm-9 col-lg-9">' +
'<h3 class="term term0">' +
lillyLivered[i].term +
'</div><div class="col-xs-3 col-sm-3 col-lg-3">' +
'<div class="col-xs-3 col-sm-3 col-lg-3">' +
' <span class="step size-32"><i class="icon ion-ios7-world"></i></span>' +
'</div></div>' +
'<div class="row dict"><div class="col-xs-12 col-sm-12 col-lg-12">' +
'<p class="definition definition0">' +
lillyLivered[i].def +
'</p><p class="eg eg0">' +
lillyLivered[i].example +
'</p></div></div></div>'
);
}
)};
}(i));
}
The other answer also mentions a good point about lillyLivered
.
Edit:
Due to high demand :D, you could also use the forEach
method to clean up your code and fix your issue:
lexicon.forEach(function(lexItem, i){
$.getJSON('http://api.urbandictionary.com/v0/define?term=' + lexItem, // lexItem === lexicon[i]
function(data){
lillyLivered = [];
lillyLivered.push(new word(data.list[0].word, data.list[0].definition, data.list[0].example));
console.log(lillyLivered);
$('.container-fluid').html('<div class="row message unread">' +
' <div class="col-xs-12 col-sm-6 col-lg-3 malok ' + color[i] + ' id="">' +
'<div class="row dict">' +
'<div class="col-xs-9 col-sm-9 col-lg-9">' +
'<h3 class="term term0">' +
lillyLivered[i].term +
'</div><div class="col-xs-3 col-sm-3 col-lg-3">' +
'<div class="col-xs-3 col-sm-3 col-lg-3">' +
' <span class="step size-32"><i class="icon ion-ios7-world"></i></span>' +
'</div></div>' +
'<div class="row dict"><div class="col-xs-12 col-sm-12 col-lg-12">' +
'<p class="definition definition0">' +
lillyLivered[i].def +
'</p><p class="eg eg0">' +
lillyLivered[i].example +
'</p></div></div></div>'
);
}
)};
});
Solution 2:
You are redefining lillyLivered
every time you call getJson
so there won't be an i
th term. Change this:
for (var i = 0; i < lexicon.length; i++) {
$.getJSON('http://api.urbandictionary.com/v0/define?term=' + lexicon[i],
function(data){
lillyLivered = [];
To:
var lillyLivered = [];
for (var i = 0; i < lexicon.length; i++) {
$.getJSON('http://api.urbandictionary.com/v0/define?term=' + lexicon[i],
function(data){...}
}
Solution 3:
If I am reading your issue correctly you want to have all of the results displayed on the page at once and you are only seeing the last one?
if that is the issue the problem is that you are overwriting your div each time you get a new item from the api. if you change the code you have that writes the html to the dom with .html:
$('.container-fluid').html(...);
to use .append instead you will see all the results on the page:
$('.container-fluid').append(...);
Post a Comment for "Why Am I Only Getting The Last Item From Javascript For Loop?"