Bases: compare list entries to string

What I’m trying to do

I’m trying to create a filter rule in bases that will loop through list entries in files and compare each of them to a string. property.contains() however only returns true if a list entry is an exact match. So it checks if the list contains the string, rather than whether any entry contains the string. An example:

Objective:

  • Find notes that have “John Doe” in their persons property(list) by searching only for “John” or “Doe”.

Problem:

  • Right now, I need to compare to the full string “John Doe” to get a match.
  • This is also true for lists of links (not just text). So [[John Doe | John]] will also only produce a match if the filter string is “John Doe”. “John” won’t suffice.

Things I have tried

I checked the list part of the bases documentation and couldn’t find anything pointing me in the right direction.

I guess the title is misleading because it might seem like list.contains() would do the job…

If the items in your list are strings, you could use:

yourList.filter(value.contains("yourSerachTerm"))

If the items in your list are something else (dates, numbers, links, objects, true/false, etc.), then go with:

yourList.filter(value.toString().contains("yourSerachTerm"))

Or use the first version but also make sure that yourSearchTerm is the same item type as the item you’re looking for.

Explanation…

When used on a string, the contains function looks in the string for substrings. That’s the one you’re trying to do.

When used on a list, the contains function looks in the list for list items, which are whole things unto themselves, be they links, dates, numbers, strings, or whatever. The function someList.contains() is not for parsing into portions of the list item.

Assuming they’re the same function is very understandable! The docs describe string.contains() and list.contains() as two different functions, but it’s still probably not very clear to everyone.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.