This doesn't appear to be related to arrays at all.
Based on your code snippet, it appears that conversationMember.Name
is expected to be a string
(due to the use of .toLowerCase()
), indicating that in this context, includes
refers to String.prototype.includes
, rather than Array.prototype.includes
. This assumption is supported by the fact that self.conversationSearchTerm
also seems to be a string (as .toLowerCase()
is called on it).
The issue arises from using includes
on something meant to be a string
but isn't. A simple solution is to default it to an empty string if it's falsy:
return (conversationMember.Name || '').toLowerCase().includes(
(self.conversationSearchTerm || '').toLowerCase()
);
Additionally, you can eliminate the need for var self = this;
as this
is accessible within your filter due to it being an arrow function. Your function, whether a computed
or a method
, could be written like this:
filteredConversations() {
return this.conversations.filter(c =>
c.MembershipData.some(md =>
(md.Name || '').toLowerCase().includes(
(this.conversationSearchTerm || '').toLowerCase()
)
)
);
}
Lastly, to handle cases where any of your conversations
lack a MembershipData
array, you can default it to an empty array dynamically:
...
(c.MembershipData || []).some(md =>
...
If a conversation lacks an array in its MembershipData
, the function will then exclude it from the result - since calling .some(condition)
on an empty array will return false.