Skip to content

Instantly share code, notes, and snippets.

@dhbalaji
Created April 13, 2020 17:41
Show Gist options
  • Save dhbalaji/fbe1cf79b80a4423e35cd3cfc7c121eb to your computer and use it in GitHub Desktop.
Save dhbalaji/fbe1cf79b80a4423e35cd3cfc7c121eb to your computer and use it in GitHub Desktop.
Deleting item on array without leaving holes in the array

Using delete on an array works but there will be holes in the array, use splice instead.

Wrong

  const arr = ['dog', 'cat', 'pig']
 delete arr[arr.length - 1];
  console.log(arr); // ['dog', 'cat', '']

Correct

  const arr = ['dog', 'cat', 'pig']
  arr.splice((arr.length -1), 1)
  console.log(arr); // ['dog', 'cat']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment