2 回答
TA贡献1824条经验 获得超8个赞
您可以将结果存储在一个常量中,以便只执行filter一次操作:
const notificationsLength = props.notifications ? Object.keys(props.notifications).filter(function (id) {
return props.notifications[id].isDiscussionType == false &&
props.notifications[id].isRead == false
}).length : 0
return (
<span className="badge badge-pill badge-primary small ml-2">
{
notificationsLength &&
<span>{notificationsLength} new</span>
}
</span>
)
TA贡献1752条经验 获得超4个赞
您可以将其提取出来并创建一个变量,例如:
const filteredNotifications = Object.keys(props.notifications)
.filter(id => props.notifications[id].isDiscussionType == false
&& props.notifications[id].isRead == false })
然后你可以做类似的事情
<span className="badge badge-pill badge-primary small ml-2">
{
filteredNotifications.length > 0 &&
(<span>{filteredNotifications.length} new </span>)
}
</span>
添加回答
举报