- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove certain number elements from an array in JavaScript
We are required to write a function that takes in an array of numbers and a number, and it should remove all the occurrences of that number from the array inplace.
Let’s write the code for this function.
We will make use of recursion to remove elements here. The recursive function that removes all occurrences of an element from an array can be written like.
Example
const numbers = [1,2,0,3,0,4,0,5]; const removeElement = (arr, element) => { if(arr.indexOf(element) !== -1){ arr.splice(arr.indexOf(element), 1); return removeElement(arr, element); }; return; }; removeElement(numbers, 0); console.log(numbers);
Output
The output in the console will be −
[ 1, 2, 3, 4, 5 ]
- Related Articles
- How to remove duplicate elements from an array in JavaScript?
- How to remove blank (undefined) elements from JavaScript array - JavaScript
- How to remove elements from an array until the passed function returns true in JavaScript?
- How to redundantly remove duplicate elements within an array – JavaScript?
- Remove elements from array using JavaScript filter - JavaScript
- Shift certain array elements to front of array - JavaScript
- How to remove an item from JavaScript array by value?
- How to remove every Nth element from an array JavaScript?
- JavaScript - How to pick random elements from an array?
- Remove elements from array in JavaScript using includes() and splice()?
- How do I recursively remove consecutive duplicate elements from an array?
- How to filter an array from all elements of another array – JavaScript?
- Shifting certain elements to the end of array JavaScript
- How to read certain number of elements from a file in Java?
- Remove number properties from an object JavaScript

Advertisements