JavaScript 是否有一组数据结构的实现?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2523436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does JavaScript have an implementation of a set data structure?
提问by Tim Molendijk
I'm looking for a decent implementation of a set data structure in JavaScript. It should be able to support elements that are plain JavaScript objects.
我正在寻找 JavaScript 中集合数据结构的体面实现。它应该能够支持纯 JavaScript 对象的元素。
So far I only found Closure Library's structs.Set, but I don't like the fact that it modifies my data.
到目前为止,我只找到了Closure Library 的 structs.Set,但我不喜欢它修改我的数据的事实。
采纳答案by Tim Down
You could build a simple wrapper around the keys of a hash table provided by my jshashtable. I have one knocking around somewhere that I will dig out later.
您可以围绕我的jshashtable提供的哈希表的键构建一个简单的包装器。我有一个在某处敲门,稍后我会挖出来。
UPDATE
更新
I have completed and tested an implementation of HashSet and uploaded it to the jshashtable project on GitHub. You can download itor view the source.
我已经完成并测试了 HashSet 的实现并将其上传到 GitHub 上的 jshashtable 项目。您可以下载它或查看源代码。
var s = new HashSet();
var o1 = {name: "One"}, o2 = {name: "Two"};
s.add(o1);
s.add(o2);
s.values(); // Array containing o1 and o2
回答by user187291
I don't think there's a way to work with object's hash code other than store it in the object itself. Strictly speaking, it's possible to create a set class without hashing, using simple linear search, but this would hardly be efficient.
我认为除了将其存储在对象本身中之外,没有其他方法可以处理对象的哈希码。严格来说,可以使用简单的线性搜索创建一个没有散列的集合类,但这几乎没有效率。
回答by CommonSenseCode
Use the ECMAScript 2015 (ES6) standard Set Data structurereally easy to use:
使用ECMAScript 2015 (ES6) 标准的 Set Data 结构非常好用:
var mySet = new Set();
mySet.add(1);
mySet.add(5);
mySet.add("some text");
var o = {a: 1, b: 2};
mySet.add(o);
mySet.has(1); // true
mySet.has(3); // false, 3 has not been added to the set
mySet.has(5); // true
mySet.has(Math.sqrt(25)); // true
mySet.has("Some Text".toLowerCase()); // true
mySet.has(o); // true
mySet.size; // 4
mySet.delete(5); // removes 5 from the set
mySet.has(5); // false, 5 has been removed
mySet.size; // 3, we just removed one value
Update for those using AngularJs
为那些使用 AngularJs 的人更新
Be aware that sets don't work with ng-repeat. So it is better you use an array and just apply a unique filter
请注意,集合不适用于ng-repeat. 所以最好使用数组并应用唯一的过滤器
回答by Salvador Dali
In ES6 version of Javascript you have built in type for set(check compatibility with your browser).
在 JavaScript 的 ES6 版本中,您已经为set内置了类型(检查与浏览器的兼容性)。
var numbers = new Set([1, 2, 4]); // Set {1, 2, 4}
To add an elementto the set you simply use .add(), which runs in O(1)and either adds the element to set (if it does not exist) or does nothing if it is already there. You can add element of any type there (arrays, strings, numbers)
要将元素添加到集合中,您只需使用.add(),它运行O(1)并添加要设置的元素(如果它不存在),或者如果它已经存在,则不执行任何操作。您可以在那里添加任何类型的元素(数组、字符串、数字)
numbers.add(4); // Set {1, 2, 4}
numbers.add(6); // Set {1, 2, 4, 6}
To check the number of elementsin the set, you can simply use .size. Also runs in O(1)
要检查集合中元素的数量,您可以简单地使用.size. 也跑进去O(1)
numbers.size; // 4
To remove the element from the setuse .delete(). It returns true if the value was there (and was removed), and false if the value did not exist. Also runs in O(1).
要从集合中删除元素,请使用.delete(). 如果该值存在(并被删除),则返回 true,如果该值不存在,则返回 false。也运行在O(1).
numbers.delete(2); // true
numbers.delete(2); // false
To check whether the element existin a set use .has(), which returns true if the element is in the set and false otherwise. Also runs in O(1).
要检查元素是否存在一组使用.has(),如果元素是集合,否则为false返回true。也运行在O(1).
numbers.has(3); // false
numbers.has(1); // true
In addition to methods you wanted, there are few additional one:
除了您想要的方法之外,还有一些其他方法:
numbers.clear();would just remove all elements from the setnumbers.forEach(callback);iterating through the values of the set in insertion ordernumbers.entries();create an iterator of all the valuesnumbers.keys();returns the keys of the set which is the same asnumbers.values()
numbers.clear();只会从集合中删除所有元素numbers.forEach(callback);按插入顺序迭代集合的值numbers.entries();创建所有值的迭代器numbers.keys();返回与集合相同的键numbers.values()
There is also a Weakset which allows to add only object-type values.
还有一个 Weakset 只允许添加对象类型的值。
回答by Lukas
I like Simple-JS-Set(probably because I wrote it). It supports any sort of JavaScript object. It has the following API:
我喜欢Simple-JS-Set(可能是因为我写的)。它支持任何类型的 JavaScript 对象。它具有以下 API:
Set(hashFunction): (Constructor) Instantiate a new set with the givenhashFunction(defaults toJSON.stringify)add(item): Add an item to the setremove(item): Remove an item from the setcontains(item): Return whether or not the item is contained in the setsize(): Return the number of unique items in the seteach(function(item), thisObj): Execute a function with each item in the set in context ofthisObj
Set(hashFunction):(构造函数)使用给定的hashFunction(默认为JSON.stringify)实例化一个新集合add(item): 将一个项目添加到集合中remove(item): 从集合中删除一个项目contains(item): 返回该项目是否包含在集合中size(): 返回集合中唯一项的数量each(function(item), thisObj): 对集合中的每个项目在上下文中执行一个函数thisObj

