Dataview metadata string length

What I’m trying to do

I am trying to get the length on metadata on a note. (The metadata is set of stars )

Things I have tried

This is currently what I have tried and I am unable to find a way to cast the metadata as a string to be run through string().lentth.

```dataview
if (j['rating'] !== undefined) {
	    let sen = " + j.rating + ";
	    const len = sen.length;
	    weight_data.push(len);
&
if (j['rating'] !== undefined) {
	    let sen = j['rating'];
	    const len = sen.length;
	    weight_data.push(len);
	    labels.push(j.file.name);
```

(pardon, I took the liberty of editing your post to add code backticks)

I appreciate it. I not sure if it matters but it dataviewjs code. Either way thank you. :slight_smile:

I have found the function I was looking for. (note renamed some var for my own easy of reading.)

dataviewjs
for (var i=0; i < latest_journals.length; i++) {
    var j = latest_journals[i];
    if (j['rating'] !== undefined) {
	    let sen = String(j['rating']);
	    const len = sen.length;
	    emto_rating.push(len);
	    labels.push(j.file.name);

The main reason your first version failed was most likely that you had it as dataview code block, and not dataviewjs codeblock.

Furthermore, best practice is to use j.hasOwnProperty('rating') to check if it has that particular key. After that it’s safe to use the j['rating'].

Bonus tip: How to present code properly in a forum post

If you want to showcase either markdown, or code blocks, or dataview queries properly in a forum post, be sure to add one line before and one life after what you want to present with four backticks, ````. This will ensure that any other backticks (like in code blocks or queries) is properly shown.

The dataview error was only on the forum side of writing, and when I noticed it after I was unable to edit. That is my foolish error on the post.

Is the j.hasOwnProperty('rating') recommended in the new code before using the let sen = String(j['rating']) to ensure that it can become a string? Cause whenever I tried using the function to count the length of a string in j['rating'] I would get a null value, and this current implementation seems to cast that value into a string so I can gets its length.

I may be misunderstanding the j.hasOwnProperty('rating') usage, please forgive me if so.

The best practice I’m referring to is related to checking the presence of the rating field in the if statement. So I think I’d rather write it something like the following:

```dataviewjs
for (const j of latest_journals) {
    if ( j.hasOwnProperty('rating') ) {
	    emto_rating.push( String(j.rating).length );
	    labels.push(j.file.name);
    }
}
```

Here I’ve simplified the for loop and also removed the declaration of variables just used once, but these are more of a personal preference. The usage of hasOwnProperty() is more compliant and safer to use when checking for a member presence like you do here.

1 Like

That clears things up, and in more ways than one.
The emto_rating.push( String(j.rating).length ); example also shows me where I made the length mistake in my other code. I was writing emto_rating.push( String(j.['rating']).length ); and getting null error.
Thank you.

These are equivalent j.rating and j['rating'], but I’m not sure what’s happening when you do j.['rating']:slight_smile:

Yeah that was my mistake! I edited it in, and just assumed dataview.

1 Like

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