javascript - 如何从 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”中删除所有空值的任何建议?
解决方案
您可以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>
))}
推荐阅读
- java - java.lang.ClassNotFoundException:com.microsoft.sqlserver.jdbc.SQLServerDrive
- c++ - 为什么我的程序不从 C++ 中的二进制文件中读取数据?
- python - 函数调用上的 For 循环 - Python
- android - startActivityForResult() 返回结果代码 RESULT_CANCELED
- django - 如何在 django 用户名中包含正斜杠
- scala - 地图中的惰性评估
- python-3.x - ibm_db 多行 sql 查询执行问题
- node.js - 使用 shell 脚本将代码复制并粘贴到 Nodejs REPL
- javascript - 将全局变量传递到函数的作用域
- php - 只选择一个表中存在于另一个表中的记录