1

I have object like this:

[{a:1, rows:[]},{a:2,rows:[]}]

I want to map the the a's as table columns and rows as cells.

I'm trying like this:

  <thead>
    <tr>
      {doc.map(({
        _id, rfqID, supplier, notes, rows,
      }) => (
          <th>{supplier}</th>
        ))}
    </tr>
  </thead>
  <tbody>
    {rows.map(({ offerPrice }) => (
      <tr>
        <td>1</td>
        <td>{offerPrice}</td>
      </tr>
    ))}
  </tbody>

But I get Uncaught ReferenceError: rows is not defined What is the correct syntax to map this table with headers & items?

2
  • 2
    syntax you got right. But the variable rows you are referring is not in the scope. Commented Oct 6, 2018 at 17:59
  • @Villemh can you please change the marked answer as the marked answer now is not correct? Commented Oct 8, 2018 at 11:22

1 Answer 1

4

Since doc is array of objects you need to do map on doc and again map on rows to display offerprice

  <thead>
    <tr>
      {doc.map(({
        _id,
        rfqID,
        supplier,
        notes,
        rows,
      }) => 
        <th key={_id}>{supplier}</th>
      )}
    </tr>
  </thead>
  <tbody>
  {doc.map(({ rows }) => 
    rows.map((offerPrice, index)  =>
      <tr key={`Key-$(index)`}>
        <td>1</td>
        <td>{offerPrice}</td>
      </tr>)
  )}
  </tbody>
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! I was trying to find a way how to get access to rows inside the same map function. Was little bit confusing. This works just fine.
@villemh Not sure this makes sense since you'll basically stack the rows for each element in your array on top of each other. It doesnt look like it will relate to your table head.
@vhflat You are absolutely right. My bad I didn't properly look at OP data. I upvoted your answer :)
I tried to delete my answer but unfortunately I am not able to do because its accepted answer

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.