Created
June 15, 2022 03:41
-
-
Save jishanshaikh4/819db2f2c551a31aa1362fce5ffc15ae to your computer and use it in GitHub Desktop.
Remove elements from an array in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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