Mimicking sets in JavaScript?

I’m working in JavaScript. I’d like to store a list of unique, unordered string values, with the following properties:

  1. a fast way to ask ‘is A in the list’?
  2. a fast way to do ‘delete A from the list if it exists in the list’
  3. a fast way to do ‘add A to the list if it is not already present’.

What I really want is a set. Any suggestions for the best way to mimic a set in JavaScript?

This question recommends using an Object, with the keys storing properties, and the values all set to true: is that a sensible way?

7 Answers
7

Leave a Comment