1 回答

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