admin管理员组

文章数量:1287490

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.

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.

Share Improve this question asked Nov 9, 2014 at 20:42 MiaMia 6,53113 gold badges53 silver badges83 bronze badges 6
  • Have you looked into sets? new Set(["one", "two", "three"]) – homam 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! – dfsq Commented Nov 9, 2014 at 21:12
  • @homam I wasn't aware of sets! I will check them! thank you. – Mia Commented Nov 9, 2014 at 21:14
  • @dfsq what is ECMAScript 5? – Mia Commented Nov 9, 2014 at 21:16
  • Similar questions: stackoverflow./questions/7958292/… stackoverflow./questions/5657219/… stackoverflow./questions/2523436/… – Stuart Commented Nov 9, 2014 at 21:16
 |  Show 1 more ment

3 Answers 3

Reset to default 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./

The most mon 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);

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.

本文标签: Data structure to store unique values in javascriptStack Overflow