Dataview: Replace number with emoji from array?

Dear Forum,

I try already an hour to make this work but without any progress :frowning: I want to replace a rating number (1-5) with its corresponding emoji in a dataview query. I tried the following (getting the corresponding emoji from an array by using the rating number as index), which doesnโ€™t work. Any idea how this can be achieved?

dataview
Table aliases as Summary, ['๐Ÿ˜ญ','๐Ÿ˜ช','๐Ÿ˜','๐Ÿ˜Š','๐Ÿ˜Ž'][rating] as Rating
FROM "Journal"

Cheers, H.

Someone might be able to give you the native method for this, but I usually find these things easier using dataviewjs rather than dataview:

```dataviewjs
dv.table(['Link', 'Rating'], dv.pages('"Journal"')
  .where(page => page.rating)
  .map(page => [page.file.link, ['๐Ÿ˜ญ','๐Ÿ˜ช','๐Ÿ˜','๐Ÿ˜Š','๐Ÿ˜Ž'][page.rating - 1]]))
```

Note that I have asked for only pages that include a rating (the .where line), and that would exclude any entry that has a rating of zero. You might need to adjust.

(I have tested the above and it functions perfectly.)

Thank you Alan, I will give it a try!

Thereโ€™s Choice to the rescue :slight_smile: Choice(boolean, true, false) can be nested.

โ€ฆdataview
TABLE aliases as Summary, choice(rating=0, :sob:, choice(rating=1, :cry:, choice(rating=2, :expressionless:, choice(rating=3, :smiling_face:, choice(rating=4, :sunglasses:, :question:))))) as Rating
FROM Journal
โ€ฆ

Do count the closing brackets, please. I always miscount.

Sorry for the โ€ฆ instead of ```, the emojis wonโ€™t render otherwise.

3 Likes

Thank you Ria for the idea with the nested choices, Iโ€™ll give it a try! Cheers, H.

Another option is to create the list of emojis and select the nโ€™th element, like in the following:

rating:: 3
```dataview 
TABLE rating, list("dummy", "a", "b", "c")[rating] as Emoji
WHERE file = this.file 
```

Should be easily adapted to your use case.

1 Like