$.array.setOperations
Set operations (Union, Intersection, Complement, Difference)
Last updated
Set operations (Union, Intersection, Complement, Difference)
Last updated
$.array.union(array1, array2, arrayN)
// Example:
const A = [1, 2, 3, 4]
const B = [2, 3]
const C = [3, 4, 5, 6]
$.array.union(A, B, C)
//=> [1, 2, 3, 4, 5, 6]
// Or
const union = Array.from(new Set([...A, ...B, ...C]))
// to check the same items inside 2 arrays
$.array.isSuperset(A, B)
//=> true$.array.intersection(array1, array2, array3)
// Example:
$.array.intersection(A, B, C)
//=> [3]
// Or just 2 arrays
const intersection = A.filter(i => B.includes(i))$.array.complement(array1, array2, arrayN)
// Example:
$.array.complement(A, B, C)
//=> [1]
// or just 2 arrays
const complement = A.filter(i => B.includes(i) === false)
// or for duplication
$.array.without(A, B)
//=> [1, 4]$.array.difference(array1, array2, arrayN)
// Example:
$.array.difference(A, B, C)
//=> [1, 2, 4, 5, 6]
or just 2 arrays (union and intersection)
const difference = union.filter(i => {
return intersection.indexOf(i) === -1
})