
- 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 in JavaScript
We are required to write a function for arrays Array.prototype.remove(). It accepts one argument; it is either a callback function or a possible element of the array. If it’s a function then the return value of that function should be considered as the possible element of the array and we have to find and delete that element from the array in place and the function should return true if the element was found and deleted otherwise it should return false.
Therefore, let’s write the code for this function −
Example
const arr = [12, 45, 78, 54, 1, 89, 67]; const names = [{ fName: 'Aashish', lName: 'Mehta' }, { fName: 'Vivek', lName: 'Chaurasia' }, { fName: 'Rahul', lName: 'Dev' }]; const remove = function(val){ let index; if(typeof val === 'function'){ index = this.findIndex(val); }else{ index = this.indexOf(val); }; if(index === -1){ return false; }; return !!this.splice(index, 1)[0]; }; Array.prototype.remove = remove; console.log(arr.remove(54)); console.log(arr); console.log(names.remove((el) => el.fName === 'Vivek')); console.log(names);
Output
The output in the console will be −
true [ 12, 45, 78, 1, 89, 67 ] true [ { fName: 'Aashish', lName: 'Mehta' }, { fName: 'Rahul', lName: 'Dev' } ]
- Related Articles
- How can I remove a specific item from an array 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?
- 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 remove a specific element from array in MongoDB?
- How to remove Specific Element from a Swift Array?
- MongoDB query to remove item from array?
- How do I remove a string from an array in a MongoDB document?
- How to remove duplicate elements from an array in JavaScript?

Advertisements