Dataview: getting a numerical value from dvjs and using it somewhere else

In general, I want to achieve a pretty simple thing. I want to track and sum up the values of a certain variable, display it in an theme-specific-overview note and then collect several of those in a table in another note.

It is for a D&D game, where I want to track the natural 20s of every character (just for context).

Things I have tried

I am very new to Javascript and Dataview Queries, so far I have managend to achieve what I want with code snippet, which I then understood, but now I have reached the limit of my understandings…

I have searched for a solution in the forum and several other sources, and most probably the answer is out there somewhere, but until now I was not able to find it…

What I’m trying to do (and have done so far)

  1. In every session note, there is the inline field critCHA:: value
  2. In the character’s overview note (say noteA), I collect and sum these up via dataviewjs and dv.pages().length
let critA = 0;
   for(let i = 0; i < dv.pages('"Games/Folder"').length; i++) {
     if(dv.pages('"Games/Folder"')[i].critCHA) {
       critA += dv.pages('"Games/Folder"')[i].critCHA;
     }
   }
dv.paragraph("myvar:: " + critA)

So far everything works and I get a display of: myvar:: value

  1. In my table in another note (say noteB) I have:
dv.table(["Name", "Value"], dv.pages("#oc/ID")
	.map(k => [k.file.link, k.myvar]))

But this returns just "+ critA and not the value of critA

  1. I then tried to parse the myvar expression to another inline field in the noteA using:
    crit:: =this.file.myvar

But this just returns a dash, replacing k.myvar with k.crit in noteB also returns a dash.

  1. Then I tried an inline field in noteA, where I put in the value manually, this was retrieved in noteB. Same thing with a value in YAML.

tl;dr:
How do I get the dataviewjs generated value in a way that I can use it somewhere else?

Dataview is used for building dynamic queries, if you want the information to be static you need to use other tools, like Templater. So if this critCHA is supposed to be a static characteristics of a character, I’d suggest looking into using Templater when creating the character, and insert it statically into that characters note page.

This would allow for queries to rely on this value later on. It can still be random, but it’s now set when the character is generated through the use of templates. (On a side note: In the template code, you could still use dataview to generate the stuff to be saved permanently).

Another question: Is there any reason why you use the for loop instead of doing the critA sum directly in a complete dataviewjs query? Is there loads of other stuff happening?

1 Like

thanks for giving me some more input and maybe I should have clarified some things.
Basically, I wanted critCHA in each session note to be static (either in YAML or inline field), the other queries should be dynamic, so that they update as the game goes on.

After a couple more hours, I have now put together a working solution (might not be the most elegant, but it works).

On the character note, I now have a bracketed Inline Field with an Inline Query that returns the sum of all values in the session notes.

[myvar:: $=let C = 0; for(let i = 0; i < dv.pages('"Games/Sessions"').length; i++) {if(dv.pages('"Games/Sessions"')[i].critCHA) {C += dv.pages('"Games/Sessions"')[i].critCHA;} else {C += 0;}}]

On the overview note I have the same table query as before, now retrieving the value from each character.

As for the for loop: yeah, I now have it in the same query that fetches the data. I had it separately first, because tried to do it stepwise. And failed :slight_smile:
I also added the else statement, because it would return - when critCHA is 0 or does not exist in a note.

In short: the bracketed inline field did the trick in the end.

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