Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Taking items from an array and inputting them into innerHTML wrapped in <p></p> and <a></a> tags with different links in each tag

I am working with an api called spoonacular.

I search some ingredients and I’m returned an array of objects containing recipes.

I would like to take a variable (the recipe title) from each object and input them into the inner HTML wrapped in

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

and tags.

The array of objects returned is something like this (this is simplified)

0
: 
{id: 661447, title: 'Square Deviled Eggs', image: 'https://spoonacular.com/recipeImages/661447-312x231.jpg', imageType: 'jpg', usedIngredientCount: 2, …}
1
: 
{id: 638035, title: 'Chicken Cordon Bleu', image: 'https://spoonacular.com/recipeImages/638035-312x231.jpg', imageType: 'jpg', usedIngredientCount: 2, …}
2
: 
{id: 641896, title: 'Easy Chicken Cordon Bleu', image: 'https://spoonacular.com/recipeImages/641896-312x231.jpg', imageType: 'jpg', usedIngredientCount: 2, …}
3
: 
{id: 652359, title: 'Monte Carlo Sandwich', image: 'https://spoonacular.com/recipeImages/652359-312x231.jpg', imageType: 'jpg', usedIngredientCount: 2, …}
4
: 
{id: 663641, title: 'Tomato tarte tatin', image: 'https://spoonacular.com/recipeImages/663641-312x231.jpg', imageType: 'jpg', usedIngredientCount: 2, …}

I currently have the titles input into the page with tags using this code where data is the array of objects:

let recipeTitles = function(){
                return data.map(el => el.title).join('</br>')
            }
            
document.querySelector('#recipeTitle').innerHTML = recipeTitles()

I thought maybe something like

return data.map(el => <p>${el.title}</p>).join('')

or

return data.map(el => `<p>`${el.title}`</p>`).join('')

Might work, but that is incorrect syntax I think now.

So i can’t figure out how to do it.

I haven’t tried to input links into the anchor tags yet because I haven’t figured out how to add the anchor tags, but I think that part won’t be too difficult using the map function?

>Solution :

You’re almost there

return data.map(el => `<p>`${el.title}`</p>`).join('')

Is incorrect syntax, but:

return data.map(el => `<p>${el.title}</p>`).join('')

Is not.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading