TL;DR;
You should not use delete
to remove an element of an array if you expect it to shrink.
Basically it will remove the reference for the first element, but won't shrink the array.
element [0] is empty -> array appears as [empty]
, because there is one empty spot in the array ! The size is still reserved in memory. And the length property is still 1.
So, this is not an empty array, it's a non-empty array with one empty spot
Other example :
var a = [3, 4];
delete a[0];
a
in my JS console results as [empty, 4]
.
Am I doing something fundamentally wrong or how do I check that the
array is empty?
If you want to check if such an array is only containing empty
s, you could could simply filter for undefined values :
var a = [3, 4];
delete a[0];
delete a[1];
if (a.filter(x => x !== undefined).length === 0) {
console.log('array looks empty!');
} else {
console.log('array doesn't look empty!');
}
(but of course this won't work if you have actual undefined
values that have some meaning, I hope it is not the case)
Maybe an explanation of typescript array memory management would be
useful.
It 's quite simple : there is no TypeScript memory management.
As already pointed out in comments, TypeScript is turned to Javascript, at runtime it is only JavaScript running, it's Javascript arrays and nothing more.
There is nothing special that Typescript is doing for you here. This will be transpiled verbatim to javascript.
For more info on the JS arrays and delete, see linked potential duplicate questions for details and explanations :