4 ms·
it's a bit of a disingenuous example because it ignores the existence of table.remove, which removes an index and reshuffles the indices to maintain the array.
by aweptimum 3y ago
it's a bit of a disingenuous example because it ignores the existence of table.remove, which removes an index and reshuffles the indices to maintain the array.
you can also swap and pop using the magic of multi assignment to preserve index order if you don't care about maintaining value order:
a[1], a[#a] = a[#a], nil
If you do care about value order there's table.sort
Just because you don't like the raw behavior doesn't mean it's incorrect. You can make the same argument about other languages in a practical use case
I'm a PHP dev and I would never rely on setting an array index to null or calling unset() on a collection if I'm deleting an element. Both lead to bugs. In the former case, null is treated as a set value so the count is inaccurate. In the latter case, the count is correct but the indices aren't continuous any more. JS has the same behavior with null and delete. Both PHP and JS have functions for manipulating arrays just as lua does.