
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How can I remove a specific item from an array JavaScript?
Let’s say, we have an array of numbers and we added elements to it. You need to devise a simple way to remove a specific element from an array.
The following is what we are looking for −
array.remove(number);
We have to use core JavaScript. Frameworks are not allowed.
Example
The code for this will be −
const arr = [2, 5, 9, 1, 5, 8, 5]; const removeInstances = function(el){ const { length } = this; for(let i = 0; i < this.length; ){ if(el !== this[i]){ i++; continue; } else{ this.splice(i, 1); }; }; // if any item is removed return true, false otherwise if(this.length !== length){ return true; }; return false; }; Array.prototype.removeInstances = removeInstances; console.log(arr.removeInstances(5)); console.log(arr);
Output
And the output in the console will be −
true [ 2, 9, 1, 8 ]
- Related Articles
- How can I remove a specific item from an array in JavaScript
- How to remove an item from JavaScript array by value?
- Remove item from a nested array by indices in JavaScript
- How do I remove a particular element from an array in JavaScript
- JavaScript Remove random item from array and then remove it from array until array is empty
- How can I delete an item from an Object in MongoDB?
- How to remove Specific Element from a Swift Array?
- MongoDB query to remove item from array?
- Remove an item from a Hashtable in C#
- How can I remove a value from an enum in MySQL?
- How to remove an item from an ArrayList in C#?
- How to remove an item from an ArrayList in Kotlin?
- How to append an item into a JavaScript array?
- How to remove a specific element from array in MongoDB?
- How to remove an item from a C# list by using an index?

Advertisements