I am encountering an issue with vs.selectedTags
array, which consists of 3 objects.
Within a for loop that iterates 3 times, I need to make 3 API calls to retrieve ticker data for each object, a task I have successfully accomplished.
The problem arises when I attempt to assign these tickers to the vs.selectedTags[i].tickers
object in the array.
The issue lies in the inability to iterate over the i
within the ApiFactory
call. As i
reaches a value of 3, I have resorted to using [i-1]
to prevent errors. However, despite this workaround, i
remains at a value of 2
, resulting in the last tickers' data being saved for all items in the vs.selectedTags
array.
var vs = $scope;
for (var i = 0; i < vs.selectedTags.length; i++) {
console.log(i);
vs.selectedTags[i].tickers = '';
console.log(vs.selectedTags[i].tickers);
ApiFactory.getTagData(vs.chosenTicker, vs.selectedTags[i].term_id).then(function(data) {
// console.log(data.data.ticker_tag);
console.log(data.data.ticker_tag.tickers);
console.log(i-1);
// console.log(vs.selectedTags[0]);
// How would you properly iterate [0 - 1 - 2] here?
vs.selectedTags[i-1].tickers = data.data.ticker_tag.tickers;
console.log(vs.selectedTags[i-1]);
});
}