Is there a library for a Set data type in Javascript?

Have a look at JS.Set.

The JS.Set class can be used to model collections of unique objects. A set makes sure that there are no duplicates among its members, and it allows you to use custom equality methods for comparison as well as JavaScript’s === operator.

It contains methods like union, intersection, merge, etc ...


Check out setjs. The API provides basic operations and the library is immutable by design.

Disclaimer: I'm the author.


Sets are now native in ES2015.

let a = new Set([1,2,3]);
let b = new Set([1,2,4]);
let intersect = new Set([...a].filter(i => b.has(i)));
let union = new Set([...a, ...b]);

This works with transpiling using babel or just natively in firefox.


If you just want to have access to simple union, intersection functions, you could also try Underscore.js's built-in Array functions. It also provides a lot of more useful utilities for data manipulation, so try it if you haven't.

Tags:

Javascript

Set