gpt4 book ai didi

javascript - 如何在 React 中使用 map 中的 map ?

转载 作者:行者123 更新时间:2023-11-30 09:19:27 26 4
gpt4 key购买 nike

我有这样的对象:

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

我想将 a 映射为表格列,将行映射为单元格。

我正在尝试这样:

  <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>

但我得到 Uncaught ReferenceError: rows is not defined用标题和项目映射此表的正确语法是什么?

最佳答案

由于 doc 是对象数组,您需要在 doc 上进行映射并再次映射到行上以显示报价

  <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>

关于javascript - 如何在 React 中使用 map 中的 map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52681823/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com