1 回答
TA貢獻(xiàn)1890條經(jīng)驗 獲得超9個贊
這似乎與數(shù)組沒有任何關(guān)系。
從你的代碼中我理解conversationMember.Name應(yīng)該是 a?string(因為你正在調(diào)用.toLowerCase()它),這意味著incudes這里不是Array.prototype.includes, but?String.prototype.includes,特別是因為它self.conversationSearchTerm似乎也是一個字符串(你也在調(diào)用.toLowerCase()它)。
所以,問題是你正在使用includes一些應(yīng)該是string但不是的東西。簡單的修復(fù)方法是當(dāng)它為假時將其默認(rèn)為空字符串:
return (conversationMember.Name || '').toLowerCase().includes(
? (self.conversationSearchTerm || '').toLowerCase()
);
附帶說明一下,您不需要var self = this;. this由于過濾器是一個箭頭函數(shù),因此在過濾器內(nèi)可用。所以你的函數(shù)(我猜它是 acomputed但它也可以是 a method)可能如下所示:
filteredConversations() {
? return this.conversations.filter(c =>?
? ? c.MembershipData.some(md =>?
? ? ? (md.Name || '').toLowerCase().includes(
? ? ? ? (this.conversationSearchTerm || '').toLowerCase()
? ? ? )
? ? )
? );
}
最后一點(diǎn):如果您中的任何一個conversations沒有MembershipData持有數(shù)組,這仍然會失敗。為了解決這個問題,您可以將其默認(rèn)為動態(tài)空數(shù)組:
?...
? ?(c.MembershipData || []).some(md =>?
?...
正如預(yù)期的那樣,任何沒有數(shù)組的對話都MembershipData將被函數(shù)過濾掉(不包含在結(jié)果中) - 因為.some(condition)在空數(shù)組上調(diào)用時將返回 false。
添加回答
舉報
