Space script indexOf(list, element)

Purpouse
Retrieve the index number of a given element in a list.

The function indefOf(list, element) would retrieve the index of the element in the list.

silverbullet.registerFunction({name: "indexOf"}, (myArray, searchTerm) => {
  return myArray.indexOf(i => i.hello === searchTerm);
})

It is the complement of the at(list, index) function present in silverbullet

at(list, index)
Returns the indexth element of `list

To test the function I use the following template to ensure that the element is present in the list but the result is the -1 of “Not Found”.

{{#let @p = {page where lastModified select name order by asc limit 5}}}
{{@p}}

{{#let @t = at(@p, 3).name}}
{{@t}}
{{indexOf(@p, @t)}}
{{/let}}
{{/let}}

What I’m doing wrong?

I’d have to look a bit deeper where the problem is, but my general debug tip would be to put console.log statements in your space script to see what values you get when functions get called. In this case, those logs should appear in your server’s logs if you run in Online mode or browser’s JavaScript console if you run in Sync mode.

I’ve found that with a array in the form:

[array1: [zero, uno, due, tre]]
[cerca: uno]

The function IndexOf return the correct value of 1

indexOf è {{indexOf(array1, cerca)}}

If I pass the @p variable the values are formatted as a table an the function in not able to find the value

{{#let @p = {page where lastModified select name order by asc limit 5}}}
{{@p}}
{{indexOf(@p, at(@p, 3).name}}
{{/let}}

function indexOf

silverbullet.registerFunction({name: "indexOf"}, (myArray, searchTerm) => {
  return myArray.indexOf(searchTerm);
})