Example on how to get Union, Intersection and Differences from two arrays using Set
A Pen by Vlad Bezden on CodePen.
| 'use strict' | |
| console.clear() | |
| const array1 = [1, 2, 3] | |
| const array2 = [2, 3, 4] | |
| const union = [...new Set([...array1, ...array2])] | |
| const intersection = [...new Set(array1.filter(a1 => array2.find(a2 => a1 === a2)))] | |
| const differences = [...new Set(array1.filter(a1 => !array2.find(a2 => a1 === a2)))] | |
| console.log(union) | |
| console.log(intersection) | |
| console.log(differences) |
Example on how to get Union, Intersection and Differences from two arrays using Set
A Pen by Vlad Bezden on CodePen.