Skip to main content
added new case for Disjunctive Union
Source Link
ifelse.codes
  • 2.4k
  • 25
  • 21

A cleaner approach in ES6 is the following solution.

var a1 = ['a', 'b']; var a2 = ['a', 'b', 'c', 'd']; 

Difference

a2.filter(d => !a1.includes(d)) // gives ["c", "d"] 

Intersection

a2.filter(d => a1.includes(d)) // gives ["a", "b"] 

Disjunctive Union (Symmetric Difference)

[ ...a2.filter(d => !a1.includes(d)), ...a1.filter(d => !a2.includes(d)) ] 

A cleaner approach in ES6 is the following solution.

var a1 = ['a', 'b']; var a2 = ['a', 'b', 'c', 'd']; 

Difference

a2.filter(d => !a1.includes(d)) // gives ["c", "d"] 

Intersection

a2.filter(d => a1.includes(d)) // gives ["a", "b"] 

A cleaner approach in ES6 is the following solution.

var a1 = ['a', 'b']; var a2 = ['a', 'b', 'c', 'd']; 

Difference

a2.filter(d => !a1.includes(d)) // gives ["c", "d"] 

Intersection

a2.filter(d => a1.includes(d)) // gives ["a", "b"] 

Disjunctive Union (Symmetric Difference)

[ ...a2.filter(d => !a1.includes(d)), ...a1.filter(d => !a2.includes(d)) ] 
Source Link
ifelse.codes
  • 2.4k
  • 25
  • 21

A cleaner approach in ES6 is the following solution.

var a1 = ['a', 'b']; var a2 = ['a', 'b', 'c', 'd']; 

Difference

a2.filter(d => !a1.includes(d)) // gives ["c", "d"] 

Intersection

a2.filter(d => a1.includes(d)) // gives ["a", "b"]