0

I have a database in MongoDB and one of the props in the document contains an array. I'm trying to map the collection in the client-side using React using this code:

<div>
    <h2>Recipes</h2>
    <ul>
        {this.state.recipes.map(recipe =>
            <li key={recipe._id}>
                {recipe.title}<br/>{recipe.category}<br/> 
                {recipe.ingredients}<br/>{recipe.preparation}
            </li>
        )}
    </ul>
</div>

ingredients prop is an array and I want to map it too. How can I do it? Any help would be great, thanks!

3
  • 1
    How the data looks like? Interested especially what the recipe.ingerdients contains. Commented Oct 1, 2020 at 12:14
  • I guess changerecipe.ingredients to {recipe.ingredients.map((ingredient) => (<p>{ingredient}</p>))} Commented Oct 1, 2020 at 12:19
  • Hi, It would be better if you include all codes from that module you have problem with Commented Oct 1, 2020 at 12:19

4 Answers 4

4

Just map it the same way you do with recipes

<div>
    <h2>Recipes</h2>
    <ul>
        {this.state.recipes.map(recipe =>
            <li key={recipe._id}>
                {recipe.title}<br/>
                {recipe.category}<br/> 
                {recipe.ingredients.map(ingredient => (
                   <span>{ingredient}</span>
                ))}
                <br/>{recipe.preparation}
            </li>
        )}
    </ul>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This is exactly what I've needed! I'll just add that in the span tag the ingredient should be surrounded by { }
3

just map it again

<div>
    <h2>Recipes</h2>
    <ul>
        {this.state.recipes.map(recipe =>
            <li key={recipe._id}>
                {recipe.title}<br/>
                {recipe.category}<br/> 
                {recipe.ingredients.map(k =>(
                   <span key={k._id}>{k}<span>
                 )<br/>
                {recipe.preparation}
            </li>
        )}
    </ul>
</div>

Comments

2

<div>
    <h2>Recipes</h2>
    <ul>
        {this.state.recipes.map(recipe =>
            <li key={recipe._id}>
                {recipe.title}<br/>{recipe.category}<br/> 
                 {  recipe.ingredients.map(ingredient => 
                    <span>{ingredient}</span>
                  )}                 
                <br/>{recipe.preparation}
            </li>
        )}
    </ul>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

1 Comment

I think forEach does not return anything, so it won't render?
1

You can also use .map() there by calling recipe.ingredients.map() as the following:

{this.state.recipes.map(recipe =>
    <li key={recipe._id}>
        {recipe.title}<br/>{recipe.category}<br/> 
        {
           recipe.ingredients.map((elem, index) => 
             <div key={index}>
                {elem /* or maybe elem['your-property-name'] if it's not a string */}
             </div>
           )
        }<br/>{recipe.preparation}
    </li>
)}

Also don't forget to add key={index} there to avoid the warning based on Lists and Keys.

1 Comment

You should add index as key to avoid the warning, you should add a unique key to avoid bugs on the ui. if you don't put a key, React automatically uses index as a key. If ingredients is a Set of ingredients he should use key={elem}. If not he should use a unique key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.