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
3 Answers
Reset to default 5You 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 itemfind
to find item's index or -1 if not found,- etc.
本文标签: Data structure to store unique values in javascriptStack Overflow
版权声明:本文标题:Data structure to store unique values in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741253559a2366242.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论