
- 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
Remove element from array referencing spreaded array in JavaScript
Suppose we have an array of literals like this −
const arr = ['cat','dog','elephant','lion','tiger','mouse'];
We are required to write a JavaScript function that takes in one such array as the first argument and then any number of strings as second and third and many more arguments.
Then our function should remove all the strings from the array taken as first argument in place if that string is provided as argument to the function.
Example
The code for this will be −
const arr = ['cat','dog','elephant','lion','tiger','mouse']; const removeFromArray = (arr, ...removeArr) => { removeArr.forEach(item => { const index = arr.indexOf(item); if(index !== -1){ arr.splice(index, 1); }; }); } removeFromArray(arr, 'dog', 'lion'); console.log(arr);
Output
The output in the console −
[ 'cat', 'elephant', 'tiger', 'mouse' ]
- Related Articles
- How to remove every Nth element from an array JavaScript?
- Remove null element from MongoDB array?
- How do I remove a particular element from an array in JavaScript
- If the element repeats, remove all its instances from array in JavaScript
- JavaScript Remove random item from array and then remove it from array until array is empty
- Remove elements from array using JavaScript filter - JavaScript
- Remove/ filter duplicate records from array - JavaScript?
- How to remove an element from an array in Java
- How to remove an element from Array List in C#?
- How to remove a specific element from array in MongoDB?
- How to remove Specific Element from a Swift Array?
- Golang Program to remove given element from the array
- Remove duplicates from array with URL values in JavaScript
- How to remove last array element in JavaScript and return it?
- How to remove first array element in JavaScript and return it?

Advertisements