Looping in a dataview query

Hey there :slight_smile:

i would like to get a list of my tasks which have a specific tag. The tag match should be detected due to a entry in a datafield in the yaml header. So here i have a list of related_topics and i would like that the query checks for each element of the list. thus the length of the related_topics list.

that is my current implementation

table
	file.etags
WHERE 
	icontains(file.tags,this.related_topics[0]) or
	icontains(file.tags,this.related_topics[1]) or
	icontains(file.tags,this.related_topics[2]) or
	icontains(file.tags,this.related_topics[3])
SORT 
	file.name ASC

but i would like to have something like that

table
	file.etags
WHERE 
for x = 0 to length(this.related_topics) 
	icontains(file.tags,this.related_topics[x])
next x
SORT 
	file.name ASC
1 Like

You could achieve your goal by using a combination of map() and either of the any(), all() & co functions. Try the following:

```dataview
TABLE file.etags 
WHERE any(map(this.related_topics, (topic) =>
  icontains(file.etags, topic) ))
SORT file.name
```

Here we loop the related topics as topic for each of them, and do icontains() on it to map it into a boolean value. After that is complete the any() will check if either is true, aka being a relevant topic. In this setting any() equals OR, all() is AND, and if didn’t want any matches you could use none().

3 Likes

great thanks again :slight_smile: