1

I have data that looks like

[{subCategory: "subCategory", name: "name", data: [{ GradeName: "grade", Population: "population", }] }] 

and I am running this code in my component

{currentCoinData.map((coin) => ( <tr key={coin.id}> <td>{coin.subCategory}</td> <td>{coin.fullName || coin.coinName}</td> <td></td> </tr> ))} 

How can I map the data key from the array of objects onto the td jsx component?

2
  • is data array always of the same size (otherwise you'll get not quite aligned table rows with different number of <td> elements in them)? Commented Aug 12, 2021 at 21:42
  • 1
    Also it looks like you didn't obfuscate your sample code carefully enough, since property names in your JSX code do not match your input object's properties. Commented Aug 12, 2021 at 21:47

2 Answers 2

2

just use another map function inside you map function:

{currentCoinData.map((coin) => ( <tr key={coin.id}> <td>{coin.subCategory}</td> <td>{coin.fullName || coin.coinName}</td> {coin.data.map(item => <td key={item.GradeName}> {item.GradeName} {item.Population} </td> )} </tr> ))} 
Sign up to request clarification or add additional context in comments.

Comments

1

You can use .map inside a .map just fine:

{currentCoinData.map((coin) => ( <tr key={coin.id}> <td>{coin.subCategory}</td> <td>{coin.fullName || coin.coinName}</td> <td>{coin.data.map(item => <span key={item.GradeName}>{item.GradeName}</span>)} </td> </tr> ))} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.