Filter out specific list in files for dataviewjs

I have a few notes with contents like below.

List1:
- item1
- item2

Other lists:
- a
- b 

I want to create a page to include all the List1 in the pages. I have tried with dv.lists(). However, it will return all the list items in the page, including items under other lists. Is it possible to only include items under List1?

I found that the items returned by dv.lists() have an attribute .list, which has the same value for the item in the same list. I can use this to distinguish different lists.

Lists in dataview carry three different links; section, header and link. The latter, link, will change if you add a blockId to that item. The header will change if the list is within some header. I’m not sure when (or what causes) section to change.

So I think the better approach would be to make sure that the list you want to target is gathered under a unique header, and that any other lists are under either no header or another header. So changing your example to the following:

## Lists

- item 1
- item 2

## Other stuff

- a 
- b

Would allow you to filter out the first list using something like:

```dataviewjs
const result = dv.pages('#f74825')
  .flatMap(p => [...p.file.lists])
  .where(i => i.header.subpath == "Lists") /* */
  .map(i => [i.header, i.text ])

dv.table(["Heading", "item.text"], result.values)
```

Another option, which I’m kind of reluctant even to present is to try to do some magic with that .list field. It seems to give the line number of the first line in your list. So it’s theoretically possible to retrieve that line (or a line relative to that line, like the line in front of it.

However, doing so relies on getting lines from that files, which I’m not sure how to do easily when just given the file link, and how many lines before are the line you’re looking for? Will it always be on the previous line, or could it be that occasionally there is an extra newline before the list begins?

So you can go down this route, but I consider it much better to actually put the list within a given logical unit using a header/section to contain the list.

1 Like

wow, thank you! that was exactly what I wanted.

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