0

I have some code which displays some data on a table be adding a new <tr><td>...

It currently display's only 1 item which is name ....

See below:

<tbody>
  {
     props.names.map( ({name}, i) => <tr key={i}><td>{ name }</td></tr> )
  }
</tbody>

If I have more items to display... for example:

id, name, lastname etc

For example:

<td>{ id }</td>
<td>{ name }</td>
<td>{ lastname }</td>

How do I modify the code above so I can do that?

3
  • Please click edit then [<>] the snippet editor and create a minimal reproducible example Commented Dec 6, 2018 at 10:44
  • It depends where you have this information stored. If your props.names is an array of objects with properties called id, name etc then yes, it's easily possible by modifying the code you have. (PS since you appear to be using React it might be a good idea to use the "react" tag) Commented Dec 6, 2018 at 10:44
  • 1
    Is that JSX? Are you working with React? Commented Dec 6, 2018 at 10:47

2 Answers 2

1

I think you could try:

props.names.map( ({id, name, lastname}, i) => ( 
    <tr key={i}>
       <td>{ id }</td>
       <td>{ name }</td>
       <td>{ lastname }</td>
     </tr> 
  ))
)

Plus this looks like react Js and not javascript, you should change your tags to get more pertinent response/help !

Sign up to request clarification or add additional context in comments.

Comments

0

if names is an array with many name objects. As an example, If a single name object looks like this? {"id":1, "name": "Tim Ering", "lastname": "Ering"}

Try this?

props.names.map( (nameObj, i) => ( 
<tr key={i}>
   <td>{ nameObj.id }</td>
   <td>{ nameObj.name }</td>
   <td>{ nameObj.lastname }</td>
 </tr> 
))

But I don't know why just props. I thought that would be this.props.names

Comments

Your Answer

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