Dataview Query that includes Files and Aliases

TLDR: I need a Dataview Query for use with Metadata Menu that shows not only the filenames of a “fileClass”, but also the aliases, and returns the file link of the file containing either.

I’m using Metadata Menu and have a fileClass called “Medications”. Each medication has a note, and the frontmatter includes fileClass: Medications and aliases: . An example note with the filename “Ibuprofen.md” might contain:

---
fileClass: Medications
title: Ibuprofin
aliases: Advil, Motrin, Nuprin
---

When I use Metadata Menu to choose a medication for a field in a note that references a medication, I’d like see a list that includes “Ibuprofen, Advil, Motrin, Nuprin”, and if I choose any of those, it would return the link to the file “Ibuprofen.md”.
I currently have the Metadata Menu “medication” field setup with a Dataview Query:
dv.pages('').where(p => p.fileClass == 'Medications').map(p => p.file.link)

This gives me a list of all the file names with a fileClass of “Medications”, but it doesn’t include the aliases for them. Is it possible to display the aliases as well as the filename, and return the file link if any of them is chosen?

Something like either of these queries? (It’s the same query, but the first variant is easier to read and presents the result in a list, and the second is an inline version, just returning the links)

```dataviewjs
dv.list( dv.pages()
  .where( p => p.fileClass == "Medications")
  .flatMap( p => {
    let links = [dv.fileLink(p.file.link.path, false, p.title)]
    for (let a of p.aliases) {
      links.push( dv.fileLink(p.file.link.path, false, a) )
    }
    return links
  })
  .sort(a => a.display)
)

```


`$= dv.pages().where( p => p.fileClass == "Medications").flatMap( p => { let links = [dv.fileLink(p.file.link.path, false, p.title)]; for (let a of p.aliases) { links.push( dv.fileLink(p.file.link.path, false, a) ) }; return links }).sort(a => a.display)` 

For my very limited medication set it displays:
image

The gist of it that for each file, we do a flatMap() which expands from one row into how many rows the returned array has. And in this particular case we build that array up from the main file link (using title for the display part), and all of the aliases using the main file link with the alias as the display part.

I feel it should be possible to shorten the building of the links array using some spread operator, or similar fun, but the queries do work as is.

Didn’t work within the Metadata Menu query, so I tried it in a post and they both error with “TypeError: p.aliases is not iterable”. Not sure why mine would behave differently than yours. My fileClass property is of type “Text” and my aliases is of course type “Aliases”. Some medications have no aliases, others have up to 4 or 5.

Oh shoot… I forgot that I changed from aliases: a, b, c to a proper list, but forgot that aliases is specially treated by Obsidian…

We’ll need to implement some split() magic on that string, to make it work. Sadly, I don’t have time right now to do that, but it should be something like: p.aliases.split(",")

Update: Hmm… My aliases changes the formatting back and forth… Need to tinker a little more with this…

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