How do you slice a list/array inside dataview query?

What I’m trying to do

While creating a new note from a template, I’ve set the alias to automatically include the file name as the first object using this a template: a prefix + tp.file.title . This allows me to retrieve this file with the file title or alt aliases.

I’m trying to list all the files generated as represented below in a dataview table

File Description Aliases

Here the problem is ‘alias’ column shows the file title which is already represented in the first column. I’d like to slice the alias to skip the first object (filetitle). I’d need to keep the filetitle in the alias list for different use cases so removing that is not an option.

Things I have tried

Here is my current query

TABLE Description,  
alias[1]
FROM #files"

I’ve tried alias[1:] and alias.slice(1) both python and javascript syntaxes and neither works. Do we have any workarounds?

I think you could use filter(), to eliminate the file title from the list. Are you familiar with how to use that?

Alternatively you could opt for a dataviewjs query, and use those slicing options directly, but filter should do the trick.

I’m not very familar with filter() yet. Pretty new with dataview.

Checking the docs for filter usage. I tried something like this but it doesn’t seem to work

TABLE Description, 
filter(alias, (x), x != this.file.name)
FROM #files

I’m guessing that I’m using filter() incorrectly. Will play around more.

I’ll keep JS as the last option lol

Would table without ID work for your needs?

---
Description: a markdown file
alias: notes, thoughts, ideas
Tags: files
---

```dataview 
TABLE 
Description, 
alias
FROM #files 
LIMIT 5
```


```dataview 
TABLE WITHOUT ID
link(file.link, alias) AS Alias, 
Description
FROM #files 
LIMIT 5
```

1 Like

Does this help at all?

---
Description: a markdown file
alias: [notes, thoughts, ideas]
Tags: files
---


```dataview 
TABLE 
	Description,
	filter(alias, (a) => a != this.file.name) as "Aliases sans file name",
	alias AS "Aliases with file name"
FROM 
	#files 
LIMIT 
	5
```

1 Like

Just a little explanation as to how the filter predicate works that @anon63144152 is using here (just as I would’ve used it for this purpose).

filter(alias, (a) => a != this.file.name )

First of all the syntax (a) => ... is a shorthand definition of a function which (in this case) takes one parameter, which loops over all the values of alias. It returns true or false, indicating whether we’re keeping or loosing this particular value in the end list, thus filtering the list.

So one way to explain the complete function:

  • filter( ... ) – We’re aiming to filter out values from somewhere
    • alias – The original list of values we want to filter
    • (a) => ... – For each of values, aka aliases, call a function where a is the current value/alias
      • a != this.file.name – The actual predicate, or filter function, where we compare the alias to the current file.name. If this boolean function returns true the value is kept, and if false we ignore it

Hence, we keep all values of alias which aren’t equal to this.file.name.

I just wanted to add this explanation, as the (a) => ... syntax is not very common for most people. Also note that what you call it, the (a), is your choice. Often we see that one letter combinations are used, which kind of indicate the list values. Like a for aliases, p for pages, f for file info, l for links, and so on. But this is a matter of personal preference, and a way to help yourself remember what kind of value are we using within this filter (or map or other functions using predicates like this is an example of)

3 Likes

Difference is that you know how it all works and can rationalize the whole process with great eloquence, whereas I just hit the keyboard until something serendipitous happens.

Another brilliant post. :star_struck:

2 Likes

Thanks @anon63144152 for the idea and @holroy for the fantastic explanation.

I tried my case and for some reason it doesn’t seem to work for me. I fixed the syntax issue with my query by changing , to =>. That did not help. I then copied @anon63144152 note with the query to my vault and that doesn’t work either. See the screenshot.
I don’t have any clue what I’m doing wrong.

1 Like

I’ve attached the markdown file with the note I copied from @anon63144152. FYI.

Filter Example.md (253 Bytes)

1 Like

Hello :wave:

In the example @holroy and I used, we are filtering an alias called ‘notes’ against a file name also called ‘notes’. This removes the title ‘notes’ from the ‘alias’ column in the Dataview table, even though it is in the YAML. We thought that was what you wanted.

So in your sample, you need to use an alias in the YAML that matches the file name, and then the query will remove the duplication.

At the moment there is no duplication in your sample as the file name doesn’t match anything in the aliases. Change the file name to ‘notes’ and the alias ‘notes’ should be removed from the list of aliases in the query.

Hope this is what you want. Apologies if I misunderstood the original question.

Oops. You are right. I’m an idiot. This is exactly what I wanted. Thanks for quickly jumping in and helping out.

1 Like

A pleasure. Enjoy.

:partying_face:

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