I need to match the values in an array called ingredientName with a corresponding array of objects. Here is how they look:
const ingredientName = ['chicken', 'cheese', 'tomato', 'lettuce'];
let imageObjects = [
{
name: 'chicken',
image: "https://spoonacular.com/cdn/ingredients_100x100/whole-chicken.jpg"
},
{
name: 'cheese',
image: "https://spoonacular.com/cdn/ingredients_100x100/cheddar-cheese.png"
},
{
name: 'tomato',
image: "https://spoonacular.com/cdn/ingredients_100x100/tomato.png"
},
{
name: 'lettuce',
image: "https://spoonacular.com/cdn/ingredients_100x100/iceberg-lettuce.jpg"
},
];
The goal is to retrieve the image associated with each ingredient from ImageObjects.
const generateDishes = () => {
for (let i = 0; i < ingredientName.length; i++) {
for (let j = 0; j < imageObjects.length; j++) {
if (ingredientName[i] == imageObjects[j].name) {
const newImage = $('<img>').attr('src', imageObjects[j].image)
$('#dishRect1').append(newImage)
}
}
}
};