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

发布于 2024-08-26 15:36:51 字数 232 浏览 4 评论 0原文

我正在寻找 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

还如梦归 2024-09-02 15:36:51

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

示例:

var s = new Set()
s.add("hello").add("goodbye").add("hello")
s.size === 2
s.has("hello") === true

为不支持的浏览器实现它的模块: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:

var s = new Set()
s.add("hello").add("goodbye").add("hello")
s.size === 2
s.has("hello") === true

A module that implements it for browsers without support: https://github.com/medikoo/es6-set

只怪假的太真实 2024-09-02 15:36:51

您可以围绕我的 jshashtable 提供的哈希表的键构建一个简单的包装器。我有一个在某个地方闲逛,稍后我会挖掘出来。

更新

我已经完成并测试了 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

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.

var s = new HashSet();
var o1 = {name: "One"}, o2 = {name: "Two"};
s.add(o1);
s.add(o2);
s.values(); // Array containing o1 and o2
秋日私语 2024-09-02 15:36:51

在 JavaScript 的 ES6 版本中,您已内置类型 set< /a> (检查与您的浏览器的兼容性)。

var numbers = new Set([1, 2, 4]); // Set {1, 2, 4}

添加元素到集合中,您只需使用.add(),它在O(1)中运行,并且可以将元素添加到集合中(如果它不存在)或者如果它已经存在则不执行任何操作。您可以在其中添加任何类型的元素(数组、字符串、数字)。

numbers.add(4); // Set {1, 2, 4}
numbers.add(6); // Set {1, 2, 4, 6}

检查集合中的元素数量,您只需使用.size即可。运行时间也是O(1)

numbers.size; // 4

从集合中删除元素,请使用.delete()。如果该值存在(并且已被删除),则返回 true;如果该值不存在,则返回 false。运行时间也是O(1)

numbers.delete(2); // true
numbers.delete(2); // false

检查元素是否存在在集合中,请使用.has(),如果元素在集合中,则返回 true,否则返回 false。运行时间也是O(1)

numbers.has(3); // false
numbers.has(1); // true

除了您想要的方法之外,还有一些其他方法:

  • 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).

var numbers = new Set([1, 2, 4]); // Set {1, 2, 4}

To add an element to 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)

numbers.add(4); // Set {1, 2, 4}
numbers.add(6); // Set {1, 2, 4, 6}

To check the number of elements in the set, you can simply use .size. Also runs in O(1)

numbers.size; // 4

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 in O(1).

numbers.delete(2); // true
numbers.delete(2); // false

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 in 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 set
  • numbers.forEach(callback); iterating through the values of the set in insertion order
  • numbers.entries(); create an iterator of all the values
  • numbers.keys(); returns the keys of the set which is the same as numbers.values()

There is also a Weakset which allows to add only object-type values.

素罗衫 2024-09-02 15:36:51

我认为除了将对象的哈希码存储在对象本身中之外,没有其他方法可以使用它。严格来说,可以使用简单的线性搜索来创建不进行散列的集合类,但这几乎没有效率。

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.

沙与沫 2024-09-02 15:36:51

使用ECMAScript 2015 (ES6) 标准集数据结构真的很容易使用:

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

针对那些使用 AngularJs 的人的更新

请注意,集合不能与 ng-repeat 一起使用。所以最好使用数组并应用唯一的过滤器

Use the ECMAScript 2015 (ES6) standard Set Data structure really easy to use:

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

Be aware that sets don't work with ng-repeat. So it is better you use an array and just apply a unique filter

冬天的雪花 2024-09-02 15:36:51

我喜欢 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 given hashFunction (defaults to JSON.stringify)
  • add(item): Add an item to the set
  • remove(item): Remove an item from the set
  • contains(item): Return whether or not the item is contained in the set
  • size(): Return the number of unique items in the set
  • each(function(item), thisObj): Execute a function with each item in the set in context of thisObj
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文