Array.filter() delete

Hello everyone,

I created a simple note app for learning purposes.

I use stable var array to save the data, now I’m stuck on how to remove the specific data from the array.

Please see the screenshot below.

It’s me again, I able to make it work now (see screenshot below).

Please let me know if you know any better solutions (I’m not 100% with my code), I really appreciate it.

1 Like

There are ways to improve this code, but the real reply should be: if you need to add or remove elements regularly, then do not use arrays! That always requires a full copy of the array (your code even performs 3 copies), which is very expensive and leads to quadratic cost.

What you want here is a map data structure, e.g., an RBMap with note ids as keys. Then you can just call the delete function on it.

3 Likes

You can also use var Array with null defaults. Instead of deleting elements, you will just set them to null.

let items : [var ?Text] = Array.init<?Text>(1000, null); 

You can go up to ~5mil elements if you need.

2 Likes

@rossberg I didn’t know that, I learned something new from your comment, thank you for that, I really appreciate it.

@infu wow, this is a clean code compared to what I did, thank you so much for this.

1 Like