How do I remove an element from an array by its index?

Hi! First post here, very new to Typst but truly loving it :).

I was expecting the array.remove() method to return the array without the removed element, but instead it returns the removed element. Is there an alternative method?

If not, how do I create a method myself? I managed to create a function that does what I want, but not a method for the array type. How do I do that? Could not find anything about that in the docs.

#let a = (1,2,3)
#a.remove(1) // Returns "2", I want (1, 3)

#a // Returns (1,3)

#a.filter(x => x != 2) // Returns (1,3) but filters by content not index.

#let remove-return(array, index) = {
  array.remove(index)
  return array
}
#let a = (1,2,3)
#remove-return(a, 1) // Works!

// #a.remove-return(1) // Error: "type array has no method remove-return"

Just for filter method, you can use method enumerate of array type to get index, for example:

#let a = (1, 2, 3)
#a.enumerate().filter(x => x.at(0) != 1).map(x => x.at(1)) // This will retrun (1, 3)
1 Like

Hello and welcome,

If not, how do I create a method myself?

You cannot create a method yourself in Typst, it should be done in the source code at GitHub - typst/typst: A new markup-based typesetting system that is powerful and easy to learn., see

How to add group function to Array method? - #2 by LordBaryhobal.

Best possible result would be

#let a = (1,2,3)
#let delete(arr, i) = arr.slice(0, i) + arr.slice(i+1)

#delete(a, 0) // (2, 3)
#delete(a, 1) // (1, 3)
#delete(a, 2) // (1, 2)
2 Likes