Dataviewjs list separate inline reference results by line

What I’m trying to do

I’m trying to use dataviewjs to make a list of all inline references related to a certain topic (e.g. Fruits) from my daily journal. I want to be able to write an inline reference, and have that block automatically listed in a separate note for that reference topic.

So if I wrote:
Fruit: Apples are great

it would go to a note titled ‘Fruit’ and the body would say “Apples are great”.

The problem I’m having is that if I have the same inline reference twice in the same note, the sentence/result is returned in the same line.

So for example if I have another inline reference in the same note later on that says

Fruit: Blueberries also good

in my ‘Fruit’ note I get this:
“Apples are great,Blueberries also good”.

But what I actually want is

  • "Apples are great
  • Blueberries also good"

My issue is the results are not separated by line, they are joined together in the same line. I think if it were a dataview query then FLATTEN might help me here(?) but this is dataviewjs query so I don’t know how to get the equivalent results.

Here’s the code I have so far:

dv.list(dv.pages('"Food"').where(p => p.Fruit).sort(p => p.file.name, 'desc').map(p => "" + p.Fruit),);

Things I have tried

  • Looking at the dataviewjs documentation
  • Youtube
  • Searched forums

Won’t you get the result you want by just removing the .map() part? If not, what’s "wrong"with that output?

1 Like

It seems you’re right, however when I did that it triggered a different problem. I can see it has had the desired effect but then all the properties for the file are listed and it’s a mess.

I think a screenshot is the best way to explain

https://imgur.com/a/pHRfrEZ

My bad, you do get to see all the file information without the map, but only for those having the fruit field inside.

Try using .flatMap(p => [p.fruit].flat()).

This strange incantation ensures that the fruit is considered a list in both the case of a single value and a list value. After it’s made into a list, we can then use flat() to remove any multiple levels of that list (which is the case if it was a list in the first place).

Thanks for your help. I ended up changing my workflow around this issue to a better solution.

FYI .flatmap…flat()) didn’t work. But no problem, I’ve moved on. Appreciate the help!

"
Evaluation Error: TypeError: dv.pages(…).where(…).sort(…).flatMap(…).flat is not a function
at eval (eval at (plugin:dataview), :1:140)
at DataviewInlineApi.eval (plugin:dataview:18638:16)
at evalInContext (plugin:dataview:18639:7)
at asyncEvalInContext (plugin:dataview:18649:32)
at DataviewJSRenderer.render (plugin:dataview:18670:19)
at DataviewJSRenderer.onload (plugin:dataview:18260:14)
at e.load (app://obsidian.md/app.js:1:1158302)
at DataviewApi.executeJs (plugin:dataview:19198:18)
at DataviewPlugin.dataviewjs (plugin:dataview:20068:18)
at eval (plugin:dataview:19967:124)"

It should work if you have a recent update of Dataview,

`$= dv.list(dv.pages('"Food"').where(p => p.Fruit).sort(p => p.file.name, 'desc').flatMap(p => [p.fruit].flat()) ) `

Anyways, glad you found a working solution for yourself! :smiley:

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