JavaScript 是否有集合数据结构的实现?
我正在寻找 JavaScript 中集合数据结构的合适实现。它应该能够支持纯 JavaScript 对象的元素。
到目前为止我只找到 Closure Library 的 structs.Set ,但我不喜欢它修改我的数据。
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.
So far I only found Closure Library's structs.Set, but I don't like the fact that it modifies my data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
ECMAScript 6 拥有它
规范:http://www.ecma-international.org/ecma-262/6.0/#sec-set-constructor"> ecma-international.org/ecma-262/6.0/#sec-set-constructor
用法:https://github.com/lukehoban/es6features#map--set--weakmap--weakset
示例:
为不支持的浏览器实现它的模块:https://github.com/medikoo/es6-set
ECMAScript 6 has it
Spec: http://www.ecma-international.org/ecma-262/6.0/#sec-set-constructor
Usage: https://github.com/lukehoban/es6features#map--set--weakmap--weakset
Example:
A module that implements it for browsers without support: https://github.com/medikoo/es6-set
您可以围绕我的 jshashtable 提供的哈希表的键构建一个简单的包装器。我有一个在某个地方闲逛,稍后我会挖掘出来。
更新
我已经完成并测试了 HashSet 的实现,并将其上传到 GitHub 上的 jshashtable 项目。您可以下载或查看源代码。
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.
UPDATE
I have completed and tested an implementation of HashSet and uploaded it to the jshashtable project on GitHub. You can download it or view the source.
在 JavaScript 的 ES6 版本中,您已内置类型 set< /a> (检查与您的浏览器的兼容性)。
要添加元素到集合中,您只需使用
.add()
,它在O(1)
中运行,并且可以将元素添加到集合中(如果它不存在)或者如果它已经存在则不执行任何操作。您可以在其中添加任何类型的元素(数组、字符串、数字)。要检查集合中的元素数量,您只需使用
.size
即可。运行时间也是O(1)
要从集合中删除元素,请使用
.delete()
。如果该值存在(并且已被删除),则返回 true;如果该值不存在,则返回 false。运行时间也是O(1)
。要检查元素是否存在在集合中,请使用
.has()
,如果元素在集合中,则返回 true,否则返回 false。运行时间也是O(1)
。除了您想要的方法之外,还有一些其他方法:
numbers.clear();
只会从集合中删除所有元素numbers.forEach(callback);
迭代按插入顺序排列的集合的值numbers.entries();
创建所有值的迭代器numbers.keys();
返回集合的键,即与numbers.values()
相同还有一个 Weakset 允许仅添加对象类型值。
In ES6 version of Javascript you have built in type for set (check compatibility with your browser).
To add an element to the set you simply use
.add()
, which runs inO(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)To check the number of elements in the set, you can simply use
.size
. Also runs inO(1)
To remove the element from the set use
.delete()
. It returns true if the value was there (and was removed), and false if the value did not exist. Also runs inO(1)
.To check whether the element exist in a set use
.has()
, which returns true if the element is in the set and false otherwise. Also runs inO(1)
.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()
There is also a Weakset which allows to add only object-type values.
我认为除了将对象的哈希码存储在对象本身中之外,没有其他方法可以使用它。严格来说,可以使用简单的线性搜索来创建不进行散列的集合类,但这几乎没有效率。
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.
使用ECMAScript 2015 (ES6) 标准集数据结构真的很容易使用:
针对那些使用 AngularJs 的人的更新
请注意,集合不能与 ng-repeat 一起使用。所以最好使用数组并应用唯一的过滤器
Use the ECMAScript 2015 (ES6) standard Set Data structure really easy to use:
Update for those using 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我喜欢 Simple-JS-Set (可能是因为我写了它)。它支持任何类型的 JavaScript 对象。它具有以下 API:
Set(hashFunction)
:(构造函数)使用给定的hashFunction
实例化一个新集合(默认为JSON.stringify
)add(item)
:向集合添加一个项目remove(item)
:从集合中删除一个项目contains(item)
:返回是否该项目是否包含在集合中size()
:返回集合中唯一项目的数量each(function(item), thisObj)
:执行函数thisObj
上下文中集合中的每个项目I like Simple-JS-Set (probably because I wrote it). It supports any sort of JavaScript object. It has the following 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