Dataviewjs flatten

What I’m trying to do

Trying to replicate the DQL flatten functionality in dataviewjs.

Things I have tried

This gets me what I want but is there a way of doing it using just the built in chain functions e.g not having to create the pages variable.

const pages = dv.pages('"Material/Comedy/jokes"')
    .where((p) => p.datetime?.year == 2024 && p.tags?.length > 0)

const flattened = pages.to("tags")
    .map((tag) => {
        const result = { key: tag, rows: [] };
        
        pages.forEach((page) => {
            if (page.tags.includes(tag)) {
                result.rows.push(page);
            }
        })

        return result;
    })

console.log(grouped);

It’s not the easiest part of the documentation, but try playing around with .flatMap() from raw interface. It should be able to do what you want.

I’ve not tried this one, but it could also be that you could use .to() also, which also does a flatten to some sort. But I’ve not tried this one, I’ve mostly used the flatMap() as the best variant of FLATTEN when in dataviewjs.

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