Search code examples
javascriptdata-structuresset

Does JavaScript have an implementation of a set data structure?


I'm looking for a decent implementation of a set data structure in JavaScript. It should be able to support elements that are plain JavaScript objects.

So far I only found Closure Library's structs.Set, but I don't like the fact that it modifies my data.


Solution

  • You could build a simple wrapper around the keys of a hash table provided by my jshashtable. I have one knocking around somewhere that I will dig out later.

    UPDATE

    I have completed and tested an implementation of HashSet and uploaded it to the jshashtable project on GitHub. You can download it or view the source.

    var s = new HashSet();
    var o1 = {name: "One"}, o2 = {name: "Two"};
    s.add(o1);
    s.add(o2);
    s.values(); // Array containing o1 and o2