Motoko syntax is very stange

I am new in Motoko language an I am trying to remove a specific item in the array but I didn’t fine a simple method like remove, delete

delete array[5];

can anyone help
Can I use another language insted of motoko such as javascript,C#…

The Motoko base library is pretty barebones right now, but I’m surprised I can’t find anything in the Array functions that allows for simple deletion like that, either by index or by value. Seems like a fairly obvious oversight. Maybe someone can chime in with a better way, but you could remove an item by filtering the array into a new one:

public func remove(array: [Text], value: Text) : [Text] {
    Array.filter(array, func(val: Text) : Bool { value != val });
}

Or possibly slicing the array to avoid the item at the desired index, but I’m not sure if array slicing is even possible in Motoko.

Yes, Array.filter works, but since array is fixed-length, inserting/deleting an element from an array takes linear time. You can use Buffer or List from the base library for efficient insertion and deletion.

3 Likes

^ @ThanasisNta (this is along a similar line to your last DM).

1 Like