Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying array of objects using map in reactJs

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.

like image 735
PremKumar Avatar asked Sep 04 '26 21:09

PremKumar


2 Answers

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>
like image 84
yotke Avatar answered Sep 06 '26 11:09

yotke


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>
like image 44
Hemadri Dasari Avatar answered Sep 06 '26 11:09

Hemadri Dasari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!