Created
August 18, 2021 15:24
-
-
Save damiancipolat/4b039cdedfc3cb04e06f2c8262b4a30f to your computer and use it in GitHub Desktop.
Javascript special array set operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Return the sets intersection. | |
Params | |
arrA : array | |
arrB : array | |
Return | |
array | |
*/ | |
const intersection = (arrA,arrB) => arrA.filter(x => arrB.includes(x)); | |
/* | |
Return the difference of two sets. | |
Params | |
arrA : array | |
arrB : array | |
Return | |
array | |
*/ | |
const difference = (arrA,arrB) => arrA.filter(x => !arrB.includes(x)); | |
/* | |
Make the simetric difference between two arrays. | |
Params | |
arrA : array | |
arrB : array | |
Return | |
array | |
*/ | |
const symDifference = (arrA,arrB) => arrA.filter(x => !arrB.includes(x)).concat(arrB.filter(x => !arrA.includes(x))); | |
/* | |
Return the union between two sets. | |
Params | |
arrA : array | |
arrB : array | |
Return | |
array | |
*/ | |
const union = (arrA,arrB) => [...arrA, ...arrB]; | |
/* | |
Return the union of two sets handling repetead elements. | |
Params | |
topic:string, | |
msg: {structure} | |
Return | |
promise | |
*/ | |
const unionUnique = (arrA,arrB)=> [...new Set([...arrA, ...arrB])]; | |
module.exports = { | |
intersection, | |
difference, | |
symDifference, | |
union, | |
unionUnique | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment