简体   繁体   English

在React中如何在map内使用map?

[英]How to use map inside map in React?

I have object like this: 我有这样的对象:

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

I want to map the the a's as table columns and rows as cells. 我想将a映射为表列,将行映射为单元格。

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? 但是我遇到了Uncaught ReferenceError: rows is not defined表与标题和项目映射的正确语法Uncaught ReferenceError: rows is not defined什么?

Since doc is array of objects you need to do map on doc and again map on rows to display offerprice 由于doc是对象数组,因此您需要在doc上进行映射,然后再次在行上进行映射以显示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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM