Dataviewjs query to show random note based on metadata

What I’m trying to do

I collect good memories in my daily note with dataview inline properties like this:

[♥:: good memory here]

I have a page where I collect all of these with the following dataview query:

TABLE ♥
WHERE ♥
SORT file.name desc

What I am trying to do now is have a block on another page that just shows one random good memory.

Things I have tried

I guess I need dataviewjs for that. I don’t understand Javascript so I searched this forum, looked at the dataview documentation and googled but couldn’t find a solution that seemed to refer to this type of metadata. I do have a query that brings up random notes to develop based on a hashtag with code that I found here and where I changed the FROM query.

let docs = dv.pages('#develop');
let length = docs.length;
let numberToReturn = 6;
var randos = getRandos(docs, length, numberToReturn);

dv.list(randos);

function getRandos(list, max, itemNum) {
  var items = [];
  for (var i=0;i<itemNum;i++) {
    items.push(list[Math.floor(Math.random() * max)].file.link);
  }
  return items;
}

Alternatives

If this doesn’t work, I would go through my daily notes and add a #memories tag to the relevant lines. I tried that with a few and used the code above with the correct tag but that (of course) results in a list of the file names and not the lines in the files, which is what I want.

Any way to do this?

In more recent version of Dataview you’re actually capable of produce the random items using the hash() function to convert text into random values.

So try adding the following to a note of yours and see if they could be useful to you:

## Adding a random value

```dataview
TABLE item, randValue 
WHERE ♥
FLATTEN flat(list(♥)) as item
FLATTEN hash(dateformat(date(today), "YYYY-MM-DD"), file.name + item) as randValue
```

##  Two random items each day

```dataview
LIST WITHOUT ID item
WHERE ♥
FLATTEN flat(list(♥)) as item
FLATTEN hash(dateformat(date(today), "YYYY-MM-DD"), file.name + item) as randValue
SORT randValue
LIMIT 2
```

The first query showcases the attached random value to each occurence of your heart field, and the second query uses LIMIT 2 to only display two of these items as it has now sorted according to that random value.

Since the hash() function as written here is using the ISO date of today, which two items will change for each day (no matter how many times you run the query). Also note that using just file.name would make every field from a given field have the same “random” value, so I added the text of the field into the mix to make it even more random.

Finally note that I’ve used the FLATTEN flat(list(...)) as item to force any single or list of fields value into becoming a row of its own, so that all fields are treated as separate entries.


This could also be done using dataviewjs, but since you say that you’re not too fluent in coding, I presented you with this solution hoping it’s easier to understand.

Perfect, thank you so much! This does exactly what I wanted and I also appreciate the added explanation of the code so I understand what I’m doing :smiley:

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