I have the following array:
var example = [
function() { /* hello */ },
function() { /* goodbye */ }
];
I use delete example[0]; and am left with the following result:
var example = [
1: function() { /* goodbye */ }
];
Is there a better solution than delete? Or is there a simple way to fix the indexes in the array? (I have jQuery at my disposal)
Edit: I fixed the example array. The indexes were there for the example.
As
exampleis an array, instead ofdelete, you can use.splice[MDN]:But if
examplesis actually an object, you’d have to usedeleteand iterate over all properties and “rename” them. Though it’s probably easier to use an array instead.The reason why you get this result with
deleteis that you are removing the property'0'from the underlying object and the property'1'still exists. You are basically operating on a lower level.Another problem with gaps in the indexes is that
array.lengthwill always return the the highest index + 1 and not the actual number of values in the array. So after deleting the element,example.lengthwould still be2.