Create image gallery - limited number from large amount of images

What I’m trying to do

I’ve got a vault that contains a large amount of images (5000+). I’d like to have a table filled with the 20 last images I added.
I use a file-naming convention using a short ‘ISO-date’, source, unique number. For example an image from Instagram, added today could be named “240405 instagram 008.jpg”

Things I have tried

I currently have the following dataviewjs-query, which is constructed from all kinds of online snippets. I do not have any experience with JS…

```dataviewjs  
// From which folder (including subfolders)?
const myFolder = "INBOX"

// How many files do you want to see?
const myLimit = 10

// Get files from "myFolder" and filter out unwanted extensions 
const mediaFiles = app.vault.getFiles()
	.filter(file => file.extension !== 'md' && file.extension !== 'mp4' && file.path.includes(myFolder))

// Sort (?) & limit number of files
const filteredFiles = dv.array(mediaFiles)
        .sort()
        .limit(myLimit) // maximaal aantal bestanden

// Create the table
dv.table(["Image", "Link"], filteredFiles
	.map(b => [
		dv.fileLink(b.path, true), 
		dv.fileLink(b.path, false, b.name)
		])
	)
```

Current Problems

First Problem - too many files

I currently have the table filtering only on “INBOX”, but I would like to get images from the whole vault. However, when I remove the filter I get RangeError: Maximum call stack size exceeded, so apparently I have to do the filtering earlier in the process (if that’s even possible).

Second Problem - how to sort

I’d like to sort the files based on their file name, in descending order

  • 240405 instagram 008.jpg
  • 240404 website 005.jpg
  • 240301 camera 105.jpg

I somehow don’t get the .sort to work

Third Problem - create an array of unwanted file extensions

To clean up the code, I’d like to make a constant with an array of unwanted file extensions (currently only .md & .mp4)

What am I looking for?

I would be very grateful if you could help me with the afore mentioned problems, but perhaps help to clean up the code as well.

Thanks in advance for any help!

Something like the following should answer your questions I think:

```dataviewjs

const myFolder = "ForumStuff"
const unwantedExt = [".md", ".mp4"]
const myLimit = 10

const mediaFiles = app.vault.getFiles()
  .filter(f => f.path.includes(myFolder) &&
    !unwantedExt.some(ext => f.path.endsWith(ext))
  )

const filteredFiles = dv.array(mediaFiles)
  .sort(b => b.name, "desc")
  .limit(myLimit)

dv.table(["Image", "Link"], filteredFiles
  .map(file => [
    dv.fileLink(file.path, true),
    dv.fileLink(file.path, false)
]))
```

The .some() function loops through the list of unwanted extensions, and checks if any of them matches with the function provided. This result is then inverted using the ! in front, and then we have a none matching function.

The sort(b => b.name, "desc") is the correct sort variant for a Dataview array, and you only need to pick out which field to sort on, and then Dataview does the rest of the magic. This is different from an ordinary array where you’d need to do something like .sort((a, b) => a <= b) where the function would need to return -1, 0 or 1 depending on the comparison used. I’m just saying this so that you’re aware that it’s a very different beast to sort using Dataview arrays as opposed to ordinary javascript arrays.

1 Like

Hi Holroy,

thanks a million! You’ve helped me with the solution and I’ve learned some dataviewjs along the way!

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