I have an array that I'm passing to a payload that will be posted to an API. In the array are field names that the API will take individually( not as an array) so I created a for loop to iterate thru the array and add the field names to the payload dynamically. But when I make the call, I get data for only the last field name. If I have say a total of 6 items in the array, I get data for just the last field.
function getData(payload, index, field) {
var deferred = $q.defer();
for (var i = 0; i < field.length; i++) {
if (field[i]) {
console.log("terms logged", field[i]);
var termsData = {
user_selection: payload,
index_info: index,
field: field[i]
};
console.log("terms data", termsData);
}
}
$http({
url: 'API',
method: "POST",
data: $.param(termsData),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function (response) {
var data = response.data;
console.log("response data", data);
deferred.resolve(data);
});
return deferred.promise;
}
Do I need to repeat the loop after the initial call? Since it's in a for loop, I assumed the calls would be made one after another until the condition is met.