Accessing YAML with DataviewJS

Hi all, wondering if I could get some help with a DataviewJS query.

What I’m trying to do

I keep a database of words I’m trying to learn, with frontmatter formatted like

word: theWord
definition: "what the word means"

I’d like to pull one random word as well as its definition into my dashboard and link back to the word’s source, so I’d get:

[[Muqarnas]]: A form of ornamented vaulting in Islamic architecture.

Things I have tried

This code gets me partway there: it shows a random word whenever the page is reloaded, but I’m not sure how to access the YAML frontmatter to pull in the definition or how to link to the page source.

const words = dv.pages('#resources/words').map(word =>;
const random = Math.floor(Math.random() * (words.length - 1));
const randomWord = words[random];
dv.paragraph('!' + randomWord + '</span>');
const links = dv.pages('#resources/words').map(x =>
const definitions = dv.pages('#resources/words').map(x => x.definition)
const random = Math.floor(Math.random() * (links.length - 1));
dv.paragraph(links[random]+": "+definitions[random]);

Try this! You can also access the word with .word the same as accessing .definition, but it seems like you were naming the file the same as the word so I simplified it to match the desired output

oh this is a bit cleaner:

const data = dv.pages('#resources/words')
const random = Math.floor(Math.random() * (data.length - 1));
dv.paragraph(data[random]": "+data[random].definition);

Do be aware that all of these solutions will provide a new word/defintion each time the query is executed. This means new words on each switch into live preview or reading mode, each the file is opened, or the view is refreshed/rebuilt.

It could be what is wanted, in that case everything is fine, but in the other case or could try looking into slightly different random generators which would only generate a new number each day (or hour) or something like that.

true! I have turned off my dataview re-evaluation and have a shortcut that inserts this templater code:
which makes life with random (and general interaction speed) overall faster, so long as I don’t mind manually refreshing

1 Like

that’s perfect, thank you so much for your help! It’s working great; I really appreciate it :slight_smile:

1 Like

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