I am working with an array of identifiers for items
var names = ['1','2', '1', '3'];
Using these ids, I send an ajax request to retrieve the name associated with each id and replace it accordingly;
var names = ['ham','cheese', 'ham', 'onion'];
Ultimately, I want to display a list like this:
ham x2, cheese, onion
But instead, I end up with:
ham x2, cheese, ham, onion
How can I achieve the desired result?
Here is my code:
var list = [];
function _checkIngredients(pizza, ingredient) {
for (var i = 0; i < list[pizza].ingredients.length; ++i) {
if (list[pizza].ingredients[i] == ingredient) {
return true;
}
}
return null;
}
pizzas.get().then(function (response) {
list = response.data;
angular.forEach(list, function (v, k) {
// Find ingredient names per pizza
angular.forEach(v.ingredients, function (i, ik) {
ingPerPizza.get(i).then(function (response) {
var name = response.data.name;
if (_checkIngredients(k, name)) {
list[k].ingredients[ik] = '2x' + name;
} else {
list[k].ingredients[ik] = name;
}
});
});
});
Thank you!