How to use filter() and concat() In Javascript
In this method, we will use the concat() method to merge the array and filter() method for removing the element which repeats.
Example:
function findElementsInArr(arr1, arr2) {
let mergedArray = arr1.concat(arr2);
let uniqueEle = mergedArray.filter(function (element, index, self) {
return self.indexOf(element) === index;
});
return uniqueEle;
}
let arr1 = [1, 2, 3, 4];
let arr2 = [3, 4, 5, 6];
let result = findElementsInArr(arr1, arr2);
console.log(result);
Output
[ 1, 2, 3, 4, 5, 6 ]
How to find every element that exists in any of two given arrays once using JavaScript ?
In this article, we will learn how to find every element that exists in any of the given two arrays. To find every element that exists in any of two given arrays, you can merge the arrays and remove any duplicate elements.
Table of Content
- Using Set
- Using loop
- Using filter() and concat()
- Using reduce and includes