3

I have 10 Array of objects like this:

Array of Objects

I tried to map ingedients and display the text:

    <ul>
        {this.props.data.ingredients.map(function(ingredient,i) { 
        return (<li key={i} item={ingredient}/>)
        })}
   </ul>

I get the result of ten bullets but the text is not being displayed. If I try ingredient.text , it says cannot display text of null.

Thanks in advance.

2 Answers 2

2

You are setting item prop with ingredient here.

return (<li key={i} item={ingredient}/>)

Where you probably want to display the text in the li, like this:

<ul>
    {this.props.data.ingredients.map(function(ingredient,i) { 
        return (<li key={i} item={ingredient}>{ingredient.text}</li>
    })}
</ul>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. Tried it and i got a error TypeError: Cannot read property 'text' of null.The value of text is a String
I made a code sandbox and it works. Take a look codesandbox.io/s/j7w9mrokyw
Thank you, that works great. It was simple mistake made in my end.
2

You can also do the same in ES6 way

let ingredientsText = [];

{this.props.data.ingredients.map((ingredient,i) => { 
   ingredientsText.push(<li key={i} item={ingredient}>{ingredient.text}</li>);
})}

<ul>
    {ingredientsText}
</ul>

Comments

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.