Help needed splitting a single tag value

What I’m trying to do

Display a single rating value in a column, when the rating is a YAML tag,

tags: source/tv rating/:star::star::star:

What I want to see is :star::star::star:

Things I have tried

Current best case query is

  seasons as Seasons,
  contains(tags,"watched") as Watched,
  string(map(file.etags, (t) => split(t, "/")[length(split(t, "/")) - 1])) AS tags
from #source/tv
where !contains(file.path, "Templates")

This produces a column with "tv,⭐️⭐️⭐️"

Maybe a replace() somewhere, but I can't find the right point.

I do have a workaround. Requires updating quite a few pages, but may ultimately be better.

The reason for using rating/… as a tag is auto-complete. Just having 1-5 stars in a Rating:: field is not as easy to remember or be flexible if I need it to be.

But if I do have a Rating field with #rating/… then I can split out the trailing values.

  seasons as Seasons,
  contains(tags,"watched") as Watched,
  split(rating,"/")[1] AS Rating
from #source/tv
where !contains(file.path, "Templates")

Try a variant of the following:

       (t) => startswith(t, "#rating")
  )[1] as Rating

This part filter out only the tags starting with #rating, which we afterwards are going to split on “/” and pull out the second element (like in your workaround). However, since filter() always returns arrays, we need to pick the first element of its result.

You might get an issue if there is no rating, but I don’t think so… (Rather, I hope you don’t, but if you miss entries (which are missing the rating tag or similar, please come back and we’ll help you sort out those issues. ))

Thank you! Works perfectly, and there is no issue with missing ratings. They simply show as ‘-’ which is fine.

I’ve been a programmer for many years, but there is something about this model, Javascript and the map function that I can’t seem to get my head around. Know exactly what I have to do. Don’t always work out how to do it.

This pattern is going into my script library for future reference. It will be well used. :grinning:

1 Like

I’m glad it worked! Just a little notice on the syntax used, since you’re a programmer, and that is one do sometimes see stuff like this in dataviewjs script code:

const result = dv.pages
  .first(p => firstfunction() )
  .second(p => secondFunction() )
  .third(p => thirdFunction() )

The example doesn’t work, but it shows the daisy chaining of a query. When doing the similar stuff within DQL (like my previous post), you’ve got to turn this into something functional with callbacks, and that kind of reverses the order of stuff, so that the bogus example above would look something like:

    first(..., firstFunction() ),
    secondFunction() ),
  thirdFunction() )

Still not a working example, but hopefully it illustrates how compared to “ordinary” daisy chaining, one needs to reverse the order to get the output to be the input of the next function within a DQL query.

1 Like

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