[英]Why does my filter not work when i add intergers?
我嘗試顯示和隱藏數組中的元素。 除了數字之外,整個過濾器都運行良好。 當我使用此過濾器時, item.sprint === 不會抓住this.state.currentSprint
並且只顯示兩者:
{
this.state.personalItems
.filter(
item => item.user == this.state.user
|| item.public == "true"
&& item.sprint === this.state.currentSprint
)
.map((l, i) => ())
}
此變量對應於 1 === 2,如下例所示:
{
this.state.personalItems
.filter(
item => item.user == this.state.user
|| item.public == "true"
&& 1 === 2
)
.map((l, i) => ())
}
我已經檢查過它們是否是整數等。有沒有人有解決問題的方法?
檢查operator precedence for && and ||
. 在您的代碼中,它將評估條件如下。
item => item.user == this.state.user
|| (item.public == "true" && item.sprint === this.state.currentSprint)
你應該使用 wrap ||
條件()
和更新條件如下。
{
this.state.personalItems
.filter(
item => (item.user == this.state.user || item.public == "true")
&& item.sprint === this.state.currentSprint
)
.map((l, i) => ())
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.