Skip to content

Instantly share code, notes, and snippets.

@jishanshaikh4
Created June 15, 2022 03:41
Show Gist options
  • Save jishanshaikh4/819db2f2c551a31aa1362fce5ffc15ae to your computer and use it in GitHub Desktop.
Save jishanshaikh4/819db2f2c551a31aa1362fce5ffc15ae to your computer and use it in GitHub Desktop.
Remove elements from an array in JavaScript
// Remove n elements from the index in_dex
let myArray = ['a', 'b', 'c', 'd', 'e', 'f'];
console.log(myArray.splice(2, 3)); // n = 2, in_dex = 3
// Removes single element
Array.prototype.delete = function(element_to_be_deleted) {
const index = this.indexOf(element);
if (index > -1) {
this.splice(index, 1);
}
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment