Deleting a specific item in an array

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By CatRass

I have code that adds a content into an array, through a series of check boxes, but want the user to be able to delete the specific item when they un-check the box. since the player can chose the options in a random order, I don’t think using the index of the array is a good solution. What would be?

Is there a reason you couldn’t use erase() to remove the item you needed to remove?

psear | 2020-06-17 10:00

Maybe I don’t understand how erase() works, but what I did was arrayName.erase(“variable I need to erase here”) and it erases the item with index 0. I also cannot use indexes to reference items, since when I remove an item the index will change, and since the options can be picked in a random order, I can’t assign items to a specific index.

CatRass | 2020-06-17 10:03

I think as mentioned in the other answer, a dictionary would work, and would also be more consistent if you had settings being enabled or disabled, rather than added or deleted. I haven’t seen your code, but I feel like that’s typically easier to manage.

psear | 2020-06-17 10:23

:bust_in_silhouette: Reply From: 1shevelov

I would use dictionary for keeping record:

var settings: {
“option1”: true,
“option2”: false,
“option3”: 1,
“option4”: 0 }

To add something just write settings.options5 = 1 and it will be created or overwritten if already present.
To delete smth use method erase(“option2”) though I think it’s better to just switch it off, e.g. settings.options2 = 0

Wow, thank you, seems a dictionary would work better in my case!

CatRass | 2020-06-17 10:05