An Interest In:
Web News this Week
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
September 20, 2021 07:01 am GMT
Original Link: https://dev.to/rajeshkumaryadavdotcom/intersection-and-union-of-array-in-javascript-2mg8
Intersection and Union of Array in JavaScript
What is union of Arrays?
Union of arrays would represent a new array combining all elements of the input arrays, without repetition of elements.
let arrOne = [10,15,22,80];let arrTwo = [5,10,11,22,70,90];// Union of Arrayslet arrUnion = [...new Set([...arrOne, ...arrTwo])];console.log(arrUnion);
What is intersection of Arrays?
The intersection of two arrays is a list of distinct numbers which are present in both the arrays. The numbers in the intersection can be in any order.
let arrOne = [10,15,22,80];let arrTwo = [5,10,11,22,70,90];// Intersection of Arrayslet arrIntersection = arrOne.filter((v) =>{ return arrTwo.includes(v);});console.log(arrIntersection);
Demo -
Original Link: https://dev.to/rajeshkumaryadavdotcom/intersection-and-union-of-array-in-javascript-2mg8
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To