After retrieving a series of objects from an API and storing them in an array, I have encountered a situation like this:
array = [
{id: 0, name: "First", relationship: "Friend"},
{id: 1, name: "Second", relationship: "Friend"}
]
Users can freely add or remove objects to this list, which is displayed in a Vue.JS DataTable. However, there is a limit of 4 objects (let's say 4 "friends") that users can have in the array.
I am looking for suggestions on how to create a function that searches the existing array (possibly populated from the API) and places the new object in the slot corresponding to the missing ID. For example, if a user deletes the object with the ID 2 and adds another one, the function should find the empty ID 2 slot in the array and insert the new object appropriately.
So far, I have tried using array.find() with conditionals to check for the presence of the specific ID value. However, this method can sometimes lead to inserting the same object multiple times. An alternative approach that I am considering involves creating a separate map containing IDs. When a user removes an object, the map would reflect the change, and vice versa when adding a new object.
If you have any suggestions or insights on how to efficiently implement this functionality, I would greatly appreciate it. Thank you!