JsonDB: Using Delete in array instead of Slice

I received an interesting issue about JsonDB not deleting correctly the index in an Array:

I thought using the delete keyword on an index in an array was deleting it (rendering its value undefined). In fact, it only set the index value to NULL.

Don’t do

var test = {array:[1,2,3]};

delete test.array[1];

console.log(test.array); //[1,  , 3]


var test = {array:[1,2,3]};

test.array.splice(1,1); //Remove from index 1, 1 item.

console.log(test.array); //[1, 3]


If you want to remove also the index from the array, use the splice. If you want to keep your index table intact, but remove only the value, use delete.

Antoine Aflalo Written by:

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *