4

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.

6
  • Have you looked into sets? new Set(["one", "two", "three"]) Commented Nov 9, 2014 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! Commented Nov 9, 2014 at 21:12
  • @homam I wasn't aware of sets! I will check them! thank you. Commented Nov 9, 2014 at 21:14
  • @dfsq what is ECMAScript 5? Commented Nov 9, 2014 at 21:16
  • Similar questions: stackoverflow.com/questions/7958292/… stackoverflow.com/questions/5657219/… stackoverflow.com/questions/2523436/… Commented Nov 9, 2014 at 21:16

3 Answers 3

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/

Sign up to request clarification or add additional context in comments.

2 Comments

Here is a Set at MDN. Support is actually pretty good already, major browsers, IE11+.
@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.
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);

2 Comments

is it better to use {} or [] in this case? (processor and memory wise)
Doesn't matter, but if you don't need keys and values, there's no good reason to use an object.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.