
- 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 elements from array using JavaScript filter - JavaScript
Suppose, we have two arrays of literals like these −
const arr1 = [4, 23, 7, 6, 3, 6, 4, 3, 56, 4]; const arr2 = [4, 56, 23];
We are required to write a JavaScript function that takes in these two arrays and filters the first to contain only those elements that are not present in the second array.
And then return the filtered array to get the below output −
const output = [7, 6, 3, 6, 3];
Example
Following is the code −
const arr1 = [4, 23, 7, 6, 3, 6, 4, 3, 56, 4]; const arr2 = [4, 56, 23]; const filterArray = (arr1, arr2) => { const filtered = arr1.filter(el => { return arr2.indexOf(el) === -1; }); return filtered; }; console.log(filterArray(arr1, arr2));
Output
This will produce the following output in console −
[ 7, 6, 3, 6, 3 ]
- Related Articles
- Remove/ filter duplicate records from array - JavaScript?
- Remove elements from array in JavaScript using includes() and splice()?
- How to remove blank (undefined) elements from JavaScript array - JavaScript
- Remove elements from a queue using Javascript
- Remove elements from a PriorityQueue using Javascript
- Remove elements from a Set using Javascript
- Remove elements from a Dictionary using Javascript
- How to filter an array from all elements of another array – JavaScript?
- Remove elements from a linked list using Javascript
- How to remove duplicate elements from an array in JavaScript?
- Filter null from an array in JavaScript?
- Remove elements from Javascript Hash Table
- How to remove an object using filter() in JavaScript?
- How to remove certain number elements from an array in JavaScript
- How to filter values from an array using the comparator function in JavaScript?

Advertisements