I want to know the right way to delete an element in sparse array but keep the indices of others the same.
I don't want to use splice as it'd change the indexes of values.
You could take the delete operator and remove the item. The place is now sparse.
var array = [, , 1, 2, 3];
console.log(array);
delete array[3];
console.log(array);
array.forEach(v => console.log(v)); // shows two elements
.as-console-wrapper { max-height: 100% !important; top: 0; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With