Are there any pitfalls for using the index on an array-of-objects as a unique, unchanging identifier for each object? Obviously it wouldn't work if I were running operations that rearranged the order of objects in the array, but if I were only 1) pushing objects, and 2) deleting objects with the delete
operator, would I ever run into a circumstance where an object would lose/shift its "id" (index)? Are there any principles that I would be wise to put into place in order to safeguard against any id mixups?
Asked
Active
Viewed 55 times
0

Brimby
- 847
- 8
- 25
-
1If you push an item, then delete that item, then push another, the index will be the same for 2 items (one of which is no longer in the array) – CertainPerformance Aug 22 '20 at 03:32
-
If you delete an item from the array, the index of every item that follows will change. – ray Aug 22 '20 at 03:38
-
2@rayhatfield Correct me if I'm wrong, but I'm pretty sure if I delete an item none of the following indexes will change. It will just leave an 'undefined' primitive at that index, holding its place. – Brimby Aug 22 '20 at 03:42
-
Another point to remember would be that the objects themselves will not "know" their own id (unless you store that information in a redundant way as an object property too). The id will only exist on the array level. – Carsten Massmann Aug 22 '20 at 03:44
-
The delete action will leave an empty space which will not be used by a later push. So that would not mess up your system. – Carsten Massmann Aug 22 '20 at 03:50
-
@CertainPerformance Actually I just tested in the console, and it turns out if you use the `delete` operator to remove the last item in an array and then push a new item to it afterward, it gets pushed to a new index past the one that was deleted. It does not take on the same index as the previous item. – Brimby Aug 22 '20 at 03:52
-
You're right on both counts @cars10m – Brimby Aug 22 '20 at 03:52
-
@Brimby Depends on what you mean by delete. If you remove it from the array via `array.splice`, then everything that follows shifts up a slot. If you delete and item via `delete array[4]` then nothing moves but the array becomes discontiguous (has holes in it, slots whose values are `undefined`). – ray Aug 22 '20 at 03:53
-
That makes sense. I edited my question to specify that I would be using the delete operator @rayhatfield – Brimby Aug 22 '20 at 03:56
-
I obviously don't know what you're trying to do, but generating an id that avoids this issue is easy enough; unless it's just a thought exercise or a bit of transient/throwaway data I'd probably use `Date.now()` or a UUID function to avoid these potential issues. – ray Aug 22 '20 at 03:59
-
Never use sparse arrays. Consider using an object, whose keys are numeric, which you increment every time you add a new object. – CertainPerformance Aug 22 '20 at 03:59
-
I'm interested in your declaration that one should never use sparse arrays. What kinds of trouble can they cause? @CertainPerformance – Brimby Aug 22 '20 at 04:02
-
They're hard to reason about. https://remysharp.com/2018/06/26/an-adventure-in-sparse-arrays – CertainPerformance Aug 22 '20 at 04:07
-
`array.length` becomes almost meaningless, iteration functions (map, reduce, filter, forEach) have to guard against undefined entries. i can't think of any good argument in favor of discontiguous arrays. it just complicates everything, makes everything harder. – ray Aug 22 '20 at 04:09
-
What do you mean by "delete"? https://stackoverflow.com/questions/5767325/how-can-i-remove-a-specific-item-from-an-array – kmoser Aug 22 '20 at 04:22