Flatten in dvjs

Things I have tried

hello,

referring to this solution of a known helper of mine:

I fiddled it to my use case.

```dataviewjs
for (let group of dv.pages()
	.where(p => p.MusType == "Vortrag")
	.sort(p => p.thema, "asc")
	.groupBy(p => p.thema).sort(g => g.key, 'desc')) {
		dv.header(3, group.key);
		dv.table(["Thema", "Jahr"],
			group.rows
				.sort(p => p.jahr, 'desc')
				.map(p => ["[[" + p.file.path + "|" + p.alias + "]]", p.jahr]))
		}

what works pretty fine, but not quite…
The subjects (thema) is a array field, which can hold many values.

What I’m trying to do

It has to be more like this:

…instead of this:

in datataview I use the FLATTEN and that works,
but in DataviewJS I have no clue to do that.
how can I implement the syntax to flatten the output?

regards

What can I say, …well, I have become weak.
For fun, I asked the great AI oracle chatopenAI…
It spat out the code without flinching.
What that means for the future - not only here in the forum - I dare not imagine. :flushed:

Well, for all those who might be looking for something similar, here is the solution:

```dataviewjs
let groupedThema = {}
dv.pages()
    .where(p => p.MusType == "Vortrag" || p.MusType == "Lesung")
    .forEach(page => {
        if(!Array.isArray(page.thema)) {
            page.thema = [page.thema];
        }
        for (let t of page.thema) {
            if (!groupedThema[t]) {
                groupedThema[t] = []
            }
            groupedThema[t].push({
                file: page.file,
                jahr: page.jahr,
                alias: page.alias
            });
        }
    });

let sortedThema = Object.keys(groupedThema).sort((a, b) => groupedThema[b].length - groupedThema[a].length)

for (let key of sortedThema) {
        dv.header(3, key + " (" + groupedThema[key].length + ")");
        dv.table(["Thema", "Jahr"],
            groupedThema[key]
                .sort((a, b) => b.jahr - a.jahr)
                .map(p => ["[[" + p.file.path + "|" + p.alias + "]]", p.jahr]));
    }

gives this output, what I can live with.

until next time, community, I will remain faithful to you, the AI thingy doesn’t sit well with me… but it’s “progress”.

Thanks to all who have nevertheless passed by…

1 Like

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