This can occur in any language that overloads hash tables to provide what is colloquially called an "array". PHP, Lua and JavaScript are three such languages. If you depend on the strict sequential behavior of a number array, then this will be an inconvenience for you. In general, behavior also provides convenience.
A classic algorithm is defined here: remove an element from the middle of the data structure, which data structure is โbetterโ: a linked list or an array?
You have to say โlinked listโ because removing the node from the linked list does not require you to move the rest of the array one index at a time. But linked lists have other pitfalls, is there also a different data structure that we can use? You can use a sparse array *.
In many languages โโthat provide this hash type of arrays, deleting any arbitrary member of the array will change the length. Unfortunately, JavaScript does not change length, so you lose a little there. But, nevertheless, the array is "shorter", at least from the point of view of Object.keys .
* Many sparse arrays are implemented using linked lists, so do not use this as a whole. However, in these languages โโthey are hash tables with predictable ordered numeric keys.
source share