How to shorten dataviewjs-Query about frontmatter

What I’m trying to do

I have a list of files with frontmatter looking like this:

---
tags: 
first: xc
second: cv
third: abc
---

(In reality, there are 20 different properties).

I want to create a list, where I collect all content from the different properties, e.g. like

First

FILENAME
xc

Second

FILENAME
cv

Things I have tried

I have found a solution, that worked, but is very lengthy if used with all properties:

```dataviewjs
const rows = ["first", "second", "third"]

const page = dv.page ("content.md")
let content

for (const row of rows){
	if (row == "first") {
	content = (first)
	} else if (row == "second") {
	content = (page.file.frontmatter.second)
	} else if (row == "third") {
	content = (page.file.frontmatter. third)
	}
	dv.header (2, elem)
	if (content != null) {
	dv.span (content)
	}
	 
}

Is there a way shorten the code by accessing directly the relevant frontmatter? Something like:


content = (page.file.frontmatter.row)

Thx
Gab

I would try to make an object and then loop over everything.
In the loop, if I encountered a new property name I would add that property to the object and place the value in a list of one element.
If I had encountered the property name before I would push the value onto the end of the relevant list.

Thanks for your suggestion.
As I’m mostly new to javascript and dataviewjs: how would that look like?

I don’t have time just now beyond hints but you’ll want to start off with dv.pages() to get the list of pages you want to work with, and you may benefit from looking at how to get ahold of an object’s keys:

You need to know the difference between an object and an array, and how to use map and push.

Good luck for now!

1 Like

I think this is what you’re trying to achieve…

content = page.file.frontmatter[row];

and even simpler…

content = page[row];

which might look something like…

```dataviewjs
const rows = ["first", "second", "third"];
const page = dv.page ("content.md");
let content;

for (const row of rows){
	content = page[row];

	dv.header (2, row);
	if (content != null) {
	dv.span (content);
	} 
}
```
1 Like

Thx. As easy as I hoped it could be.

1 Like

oh I thought you were trying to run over all of the files in one go! well, glad you got an answer

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