

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- How to remove duplicate elements from an array in JavaScript?
- How to remove blank (undefined) elements from JavaScript array - JavaScript
- Remove elements from array using JavaScript filter - JavaScript
- Shift certain array elements to front of array - JavaScript
- How to remove false values from an array in JavaScript?
- Remove number properties from an object JavaScript
- JavaScript - How to pick random elements from an array?
- How to redundantly remove duplicate elements within an array – JavaScript?
- How to remove an item from JavaScript array by value?
- How to remove every Nth element from an array JavaScript?
- How to read certain number of elements from a file in Java?
- Shifting certain elements to the end of array JavaScript
- How do I recursively remove consecutive duplicate elements from an array?
- How to exclude certain values from randomly generated array JavaScript
- Remove elements from array in JavaScript using includes() and splice()?
Advertisements