3

What is the best data type to store unique values only? an array can have duplicates

[one, one, two]

and an object (? maybe wrong terminology) have unnecessary values for my current case

{one: something, two: something, three: something}

Shortly, I need something like this:

{one, two, three}

I am not sure what it is called, or if it does exist in js. Needing some enlightment.

Mia
  • 6,220
  • 12
  • 47
  • 81
  • Have you looked into sets? `new Set(["one", "two", "three"])` – homam Nov 09 '14 at 21:07
  • @homam Set is pretty cool and is exactly what OP might want. But it's ECMAScript 5 proposal. But soon it will be really awesome thing! – dfsq Nov 09 '14 at 21:12
  • @homam I wasn't aware of sets! I will check them! thank you. – Mia Nov 09 '14 at 21:14
  • @dfsq what is ECMAScript 5? – Mia Nov 09 '14 at 21:16
  • Similar questions: http://stackoverflow.com/questions/7958292/mimicking-sets-in-javascript http://stackoverflow.com/questions/5657219/set-of-objects-in-javascript http://stackoverflow.com/questions/2523436/javascript-implementation-of-a-set-data-structure – Stuart Nov 09 '14 at 21:16
  • @Zettam ECMAScript 6 of course. – dfsq Nov 09 '14 at 21:18

3 Answers3

5

You mean a structure called Set, and in the current version of ECMAScript there's no such structure. It will be standarized in the next version, however it's available now in some browsers.

You can emulate set using object, but as you said that also involves unnecessary values. If you don't want to care about them, you can use a library that emulates Set, like http://collectionsjs.com/

Kuba Jagoda
  • 4,908
  • 2
  • 19
  • 21
  • Here is a [Set at MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set). Support is actually pretty good already, major browsers, IE11+. – dfsq Nov 09 '14 at 21:18
  • @dfsq Yet I still wouldn't recommend using it on production. Especially that Chrome supports it only after the user turns on a proper flag. – Kuba Jagoda Nov 09 '14 at 21:24
4

The most common way to solve this is to use an array, and just check if it already has the value you want to insert, that way it contains only unique values.

if ( arr.indexOf(value) == -1 ) arr.push(value);
adeneo
  • 312,895
  • 29
  • 395
  • 388
0

In addition to obvious ways you can always create you own data structure on top of array if you need some more advanced functionality. For example:

function UArray(val) {
    this._values = [];
    if (typeof val !== 'undefined') {
        this.set(val);
    }
}
UArray.prototype.set = function(values) {
    this._values = this._values.concat(values).filter(function(el, i, arr) {
        return arr.indexOf(el) === i;
    });
};
UArray.prototype.get = function() {
    return this._values;
}

var uarr = new UArray();
uarr.set(['one', 'one', 'two']);

alert( uarr.get() );

uarr.set('two');
uarr.set(['three', 'one']);

alert( uarr.get() );

Such a custom data structure could be extended with additional necessary methods, i.e.:

  • remove to remove specific item
  • find to find item's index or -1 if not found,
  • etc.
dfsq
  • 191,768
  • 25
  • 236
  • 258