2 回答

TA貢獻1824條經驗 獲得超8個贊
您可以將結果存儲在一個常量中,以便只執(zhí)行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個贊
您可以將其提取出來并創(chuàng)建一個變量,例如:
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>
添加回答
舉報