JavaScript 是否有一个集合数据结构的实现?

2022-01-17 00:00:00 data-structures set javascript

我正在寻找一个用 JavaScript 实现的集合数据结构的体面实现.它应该能够支持纯 JavaScript 对象的元素.

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.

到目前为止,我只找到了 Closure Library 的 structs.Set,但是我不喜欢它修改我的数据这一事实.

So far I only found Closure Library's structs.Set, but I don't like the fact that it modifies my data.

推荐答案

您可以围绕我的 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.

更新

我已经完成并测试了 HashSet 的实现,并将其上传到 GitHub 上的 jshashtable 项目.你可以下载或者查看源码.

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.

var s = new HashSet();
var o1 = {name: "One"}, o2 = {name: "Two"};
s.add(o1);
s.add(o2);
s.values(); // Array containing o1 and o2

相关文章