首页 > 解决方案 > 如何从 props.map 中删除空值

问题描述

我正在从道具映射到“lawList”,但该数组包含很多我希望阻止的 nullValues。

{props.map((lawList, index) => (
  <Table.Body key={index}>
    <Table.Row>
      <Table.Cell>{index + 1}</Table.Cell>
      <Table.Cell>{lawList.lawDTO.name}</Table.Cell>
      <Table.Cell>{lawList.text}</Table.Cell>
      <Table.Cell>{lawList.status}</Table.Cell>
      <Table.Cell>
        {new Date(lawList.latestRevisionDate).toISOString().substring(0, 10)}
      </Table.Cell>
      <Table.Cell>placeholder</Table.Cell>
    </Table.Row>
  </Table.Body>
))}

关于如何从“lawList”中删除所有空值的任何建议?

标签: javascriptreactjs

解决方案


您可以filter(Boolean)在数组上使用来过滤掉任何虚假元素。

{props.lawList.filter(Boolean).map((lawList, index) => (
  <Table.Body key={index}>
    <Table.Row>
      <Table.Cell>{index + 1}</Table.Cell>
      <Table.Cell>{lawList.lawDTO.name}</Table.Cell>
      <Table.Cell>{lawList.text}</Table.Cell>
      <Table.Cell>{lawList.status}</Table.Cell>
      <Table.Cell>
        {new Date(lawList.latestRevisionDate).toISOString().substring(0, 10)}
      </Table.Cell>
      <Table.Cell>placeholder</Table.Cell>
    </Table.Row>
  </Table.Body>
))}

推荐阅读