The JavaScript Set strategies at the moment are a part of Baseline  |  Weblog  |  net.dev

0
44
The JavaScript Set methods are now part of Baseline  |  Blog  |  web.dev


Adriana Jara

Now you can use the JavaScript Set
strategies

to carry out set operations like
intersection,
union
and extra.

Units are a necessary information construction in any programming language. Now you’ll be able to
use JavaScript’s built-in strategies to carry out set operations. Simplify your set
operations utilizing the next strategies:

intersection()

intersection()
returns a brand new set containing parts in each this set and the given set.

const odds = new Set([1, 3, 5, 7, 9]);
const squares = new Set([1, 4, 9]);
console.log(odds.intersection(squares)); // Set(2) { 1, 9 }

union()

union()
returns a brand new set containing all parts on this set and the given set.

const evens = new Set([2, 4, 6, 8]);
const squares = new Set([1, 4, 9]);
console.log(evens.union(squares)); // Set(6) { 2, 4, 6, 8, 1, 9 }

distinction()

distinction()
returns a brand new set containing parts on this set however not within the given set.

const odds = new Set([1, 3, 5, 7, 9]);
const squares = new Set([1, 4, 9]);
console.log(odds.distinction(squares)); // Set(3) { 3, 5, 7 }

symmetricDifference()

symmetricDifference()
returns a brand new set containing parts which are in both this set or the given
set, however not in each.

const evens = new Set([2, 4, 6, 8]);
const squares = new Set([1, 4, 9]);
console.log(evens.symmetricDifference(squares)); // Set(5) { 2, 6, 8, 1, 9 }

isSubsetOf()

isSubsetOf()
returns a boolean indicating if all parts of this set are within the given set.

const fours = new Set([4, 8, 12, 16]);
const evens = new Set([2, 4, 6, 8, 10, 12, 14, 16, 18]);
console.log(fours.isSubsetOf(evens)); // true

isSupersetOf()

isSupersetOf()
returns a boolean indicating if all parts of the given set are on this set.

const evens = new Set([2, 4, 6, 8, 10, 12, 14, 16, 18]);
const fours = new Set([4, 8, 12, 16]);
console.log(evens.isSupersetOf(fours)); // true

isDisjointFrom()

isDisjointFrom()
Returns a boolean indicating if this set has no parts in frequent with the
given set.

const primes = new Set([2, 3, 5, 7, 11, 13, 17, 19]);
const squares = new Set([1, 4, 9, 16]);
console.log(primes.isDisjointFrom(squares)); // true

Updating your code to make use of the built-in strategies improves efficiency and reduces
technical debt.



Supply hyperlink

LEAVE A REPLY

Please enter your comment!
Please enter your name here