Limit number of elements in an array that are shown

Hello, dear Obsidians :raised_hand:

This should have a simple solution, but for the life of me I could not find it anywhere online.

I have the following dataview query, which works perfectly:

TABLE WITHOUT ID
link(file.link, title) AS "Title",
date AS "Date",
journal AS "Journal"
FROM "references"
WHERE contains(item, "journalArticle")

The problem is that the key author is a list, and the articles in my vault have a varied number of authors. I would like to limit this element in my table to the first six authors (ideally it should finish the array by saying ‘et al’ when there are more than six authors, but this is not mandatory) to prevent articles with large numbers of authors from taking too much row space. Do you know the solution?

Thanks in advance!

That query doesn’t list any authors at all, where do they come in to play?

My apologies, I issued the wrong query. This is the one:

TABLE WITHOUT ID
link(file.link, title) AS "Title",
author AS "Authors",
date AS "Date",
journal AS "Journal"
FROM "references"
WHERE contains(item, "journalArticle")

Currently there is no slice() operation available in dataview, but I’m contemplating on building one and submit a PR for it. That would allow you to slice out a part of any list.

But we’ve got a sneaky way which is possible to use (but could be a bit tiresome if you want to have many elements from a given list), and that is to remake the list and remove any null entries.

Your query would then look something like this:

```dataview
TABLE WITHOUT ID
link(file.link, title) AS "Title",
shorterAuthors AS "Authors",
date AS "Date",
journal AS "Journal"
FROM "references"
WHERE contains(item, "journalArticle")
FLATTEN list(nonnull(list(
  authors[0], authors[1], authors[2],
  authors[3], authors[4], authors[5],
  choice(length(authors) > 6, "et al", null)
))) as shorterAuthors
```

This will return up to six authors, or if there are more than 6 authors it’ll return the first six and “et al” as the 7th author.

2 Likes

Thank you so much, holroy! So I wasn’t crazy for trying to find a solution online and not having success. Guess this wasn’t as easy as I thought it would be. No way I could have reached your solution in a short time :sweat_smile:

I adapted your code to my vault, and I confirm that it works as intended! Here is the solution to by query:

TABLE WITHOUT ID
link(file.link, title) AS "Title",
author AS "Authors",
date AS "Date",
journal AS "Journal"
FROM "references"
WHERE contains(item, "journalArticle")
FLATTEN list(nonnull(list(
  author[0], author[1], author[2],
  author[3], author[4], author[5],
  choice(length(author) > 6, "Et al.", null)
))) as author

Once again, thank you. You spared me many working hours of search :pray:

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