How to Filter an Array from all Elements of another Array In JavaScript?
Last Updated :
22 Aug, 2024
Filtering one array from all elements of another array is a common task in JavaScript. This involves removing elements from one array that are present in another array. In this article we will explore different approaches to filter an array from all elements of another array in JavaScript.
These are the approaches to filter an array from all elements of another array in JavaScript
Using for Loop
In this approach we will use a for loop to iterate through the first array (arr1) and check if each element’s presence in the second array (arr2) using includes.
Example: In this example we are using for loop to filter an array from all elements of another array in JavaScript. The final array will be shown in the console.
JavaScript
const arr1 = [10, 20, 30, 40, 50];
const arr2 = [30, 50];
const result = [];
for (const item of arr1) {
if (!arr2.includes(item)) {
result.push(item);
}
}
console.log(result);
Output:
[ 10, 20, 40 ]
Using filter and includes Methods
In this approache we will use the filter and includes methods. The filter method goes through each element of the array and applies a test function to determine if the element should be included in the new array. The includes method checks if a specific value exists in an array.
Example: In this example we are using filter and includes Methods to filter an array from all elements of another array in JavaScript.
JavaScript
const arr1 = ['a', 'b', 'c', 'd', 'e'];
const arr2 = ['c', 'd', 'e'];
const result = arr1.filter(item => !arr2.includes(item));
console.log(result);
Output:
[ 'a', 'b' ]
Using Set
In this approach we will use a Set which is an efficient way to filter elements because lookup operations in a Set are generally faster compared to arrays.
Example: In this example we are using set to filter an array from all elements of another array in JavaScript. The final array will be shown in the console.
JavaScript
const array1 = [1, 2, 3, 4, 5];
const array2 = [2, 4];
const filteredArray = array1.filter(item => !new Set(array2).has(item));
console.log(filteredArray);
Output:
[ 1, 3, 5 ]
Using reduce Method
Using reduce method allows us to accumulate a result by iterating through the array, which can also be used to filter out elements.
Example: In this example we are using reduce method to filter an array from all elements of another array in JavaScript. The final array will be shown in the console.
JavaScript
const array1 = [1, 2, 3, 4, 5];
const array2 = [2, 4];
const filteredArray = array1.reduce((acc, item) => {
if (!array2.includes(item)) {
acc.push(item);
}
return acc;
}, []);
console.log(filteredArray);
Output:
[ 1, 3, 5 ]
Using map for Additional Operations
In this approach we will use the map which creates a new array by applying a function to each element of the original array. map is not typically used for filtering but we can combine it with filter to perform additional operations on the filtered results.
Example: In below example we are filtering array and also we are performing multiplication operation on filtered array using map. The final array will be shown in the console.
JavaScript
const array1 = [1, 2, 3, 4, 5];
const array2 = [2, 4];
const filteredArray = array1
.filter(item => !array2.includes(item))
.map(item => item * 2);
console.log(filteredArray);
Output:
[ 2, 6, 10 ]
Using forEach Method
The forEach method provides a straightforward way to iterate over each element of an array and perform operations. Unlike map or filter, forEach does not return a new array by itself, instead we have to manually create and populate a new array during the iteration.
Example: In this example we are using forEach method to filter an array from all elements of another array in JavaScript. The final array will be shown in the console.
JavaScript
const array1 = [1, 2, 3, 4, 5];
const array2 = [2, 4];
const filteredArray = [];
array1.forEach(item => {
if (!array2.includes(item)) {
filteredArray.push(item);
}
});
console.log(filteredArray);
Output:
[ 1, 3, 5 ]
Similar Reads
JavaScript - How to Remove an Element from an Array? Removing elements from an array is a fundamental operation in JavaScript, essential for data manipulation, filtering, and transformation. This guide will explore different methods to efficiently remove elements from an array, enhancing your understanding and capability in handling arrays.1. Using po
3 min read
How to Get all Elements Except First in JavaScript Array? Here are the various methods to get all elements except the first in JavaScript Array1. Using for loopWe will use a for loop to grab all the elements except the first. We know that in an array the first element is present at index '0'. JavaScriptconst a1 = [1, 2, 3, 4, 5]; const a2 = []; let k = 0;
4 min read
How to Remove duplicate elements from array in JavaScript ? In this article, we will discuss the methods to remove duplicate elements from a Javascript array. There are various methods to remove duplicates in the array. These are the following ways: Table of Content Using filter() MethodUsing set() MethodUsing forEach() MethodUsing reduce() MethodUsing index
4 min read
How to Check if an Element Exists in an Array in JavaScript? Given an array, the task is to check whether an element present in an array or not in JavaScript. If the element present in array, then it returns true, otherwise returns false.The indexOf() method returns the index of first occurance of element in an array, and -1 of the element not found in array.
2 min read
How to Filter an Array in JavaScript ? The array.filter() method is used to filter array in JavaScript. The filter() method iterates and check every element for the given condition and returns a new array with the filtered output.Syntaxconst filteredArray = array.filter( callbackFunction ( element [, index [, array]])[, thisArg]);Note: I
2 min read
How to get the elements of one array which are not present in another array using JavaScript? The task is to get the elements of one array that are not present in another array. Here we are going to use JavaScript to achieve the goal. Here are a few techniques discussed. Approach Take the arrays in variables.Use the .filter() method on the first array and check if the elements of the first a
2 min read
How to find Objects Containing Matching Elements from Arrays in Another Object ? The task is to find objects that contain matching elements from arrays in another object. An object containing an array of values and a separate object. The objective is to determine whether the values present in the array are also present as keys in the separate object. These are the following meth
4 min read
How to Remove Multiple Elements from Array in JavaScript? Here are various methods to remove multiple elements from an array in JavaScript1. Using filter() MethodThe filter() method creates a new array with the elements that pass the condition. This method does not change the original array.JavaScriptlet a = [1, 2, 3, 4, 5]; let remove = [2, 4]; a = a.filt
3 min read
JavaScript - Match Values from Another Array and Assign to Object of Arrays Here are the different methods to match values from another array and assign to object of arrays in JavaScript1. Using forEach() and push()In this method, we will be using the forEach() and push() methods of the array to get the same value from another array and assign it to the object of arrays.Jav
3 min read
How to check whether an array is subset of another array using JavaScript ? The task is to check whether an array is a subset of another array with the help of JavaScript. There are a few approaches, we will discuss below: Approaches:using JavaScript array.every() methodusing JavaScript array.some() methodApproach 1: Using JavaScript array.every() methodThis approach checks
2 min read