Removing an array element by name?

Hello,

what is the most efficient way to properly remove an array element based on its name?

table.remove() sadly only works with indexes, so I would have to run a method which finds an elements index in a list by using its name first, but this doesn’t seem to efficient as I would have to scan through a list everytime I want to remove something.

Are there any better alternatives?

2 Likes

Could you provide an example of your array? If it’s a table, you could use table.findand then pass it in to table.remove since table.find returns an index (keep in mind table.find can nil if the passed in value isn’t in the table, so check if it’s not nil before removing something from table)

If it’s a dictionary you can set the key’s value (assuming name is they key) to nil

But doesn’t settings the key’s value to nil clog up memory or something of the sort, because setting a dictionaries value to nil doesn’t really remove it?

So you mean like table[“Goblin”] goblin being the key and you want to remove it. Well you can remove it by making it equal to nil. This is the way i do it.

2 Likes