Delete from Array
Arrays in JavaScript are regular objects, and items can be deleted using the delete keyword. However, this does not change the length of the array and leaves a hole of empty. In other languages, this is similar to a sparse array. The empty holes are skipped when using traversal or mutation operations.
const names = ['Jack', 'Laura', 'Paul', 'Megan'];
delete names[1];
names;
// => ["Jack", empty, "Paul", "Megan"]
names.length;
// => 4
names.forEach((name) => console.log(name));
// => Jack
// => Paul
// => Megan
If there should be no holes, and if the length should reflect the number of items that will be traversed or mutated, use splice instead.
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.1
For example:
const names = ['Jack', 'Laura', 'Paul', 'Megan'];
names.splice(1, 1);
names;
// => ["Jack", "Paul", "Megan"]
names.length;
// => 3
names.forEach((name) => console.log(name));
// => Jack
// => Paul
// => Megan