Javascript 中有 Set 数据类型的库吗?

发布于 2024-08-22 22:28:40 字数 65 浏览 1 评论 0原文

由于 Javascript 没有内置的集合数据类型,有没有人遇到过一个像样的集合库和集合操作(​​如并集、交集等)?

Since Javascript doesn't have a built in set datatype has anyone come across a decent library for sets and set operations like union, intersection, etc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

鱼忆七猫命九 2024-08-29 22:28:41

immutable-js 公开了强大的设置 数据结构。

一个简单的 Node.js 示例,您可以在此处查看。

im = require("immutable")

const mySet = im.Set([1, "a", {value: Symbol()}])

// the .add and .delete methods do not modify mySet, but return a new set instance instead.
const newSet = mySet
                .add(42)
                .delete(1)

console.info("Does mySet have 42?", mySet.has(42))
console.info("Does newSet have 42?", newSet.has(42))

immutable-js expose a powerfull set data-structure.

A simple example for node.js, which you can see in work here.

im = require("immutable")

const mySet = im.Set([1, "a", {value: Symbol()}])

// the .add and .delete methods do not modify mySet, but return a new set instance instead.
const newSet = mySet
                .add(42)
                .delete(1)

console.info("Does mySet have 42?", mySet.has(42))
console.info("Does newSet have 42?", newSet.has(42))
书间行客 2024-08-29 22:28:41

就像已经提到的那样,现在 JS 上有 Set。根据您的目的,您可能还对 https://github.com/fsvieira/cset 感兴趣(我是它的作者),它是一个惰性集合库,具有常见的集合操作,包括笛卡尔积。

like have been mention there is now Set on JS. Depending on your purposes you also may be interested on https://github.com/fsvieira/cset (I am its author), its a lazzy set lib, with common set operation, including cartesian product.

不打扰别人 2024-08-29 22:28:40

看看 JS.Set

JS.Set 类可用于对唯一对象的集合进行建模。集合确保其成员之间没有重复项,并且它允许您使用自定义相等方法以及 JavaScript 的 === 运算符进行比较。

它包含并集、交集、合并等方法......

Have a look at JS.Set.

The JS.Set class can be used to model collections of unique objects. A set makes sure that there are no duplicates among its members, and it allows you to use custom equality methods for comparison as well as JavaScript’s === operator.

It contains methods like union, intersection, merge, etc ...

叶落知秋 2024-08-29 22:28:40

如果您只想访问简单的并集、交集函数,您也可以尝试 Underscore.js' s 内置数组函数。它还提供了许多更有用的数据操作实用程序,如果您还没有,请尝试一下。

If you just want to have access to simple union, intersection functions, you could also try Underscore.js's built-in Array functions. It also provides a lot of more useful utilities for data manipulation, so try it if you haven't.

悲喜皆因你 2024-08-29 22:28:40

Set 现在是 ES2015 中原生的。

let a = new Set([1,2,3]);
let b = new Set([1,2,4]);
let intersect = new Set([...a].filter(i => b.has(i)));
let union = new Set([...a, ...b]);

这适用于使用 babel 进行转译或仅在 Firefox 中进行转译。

Sets are now native in ES2015.

let a = new Set([1,2,3]);
let b = new Set([1,2,4]);
let intersect = new Set([...a].filter(i => b.has(i)));
let union = new Set([...a, ...b]);

This works with transpiling using babel or just natively in firefox.

我的奇迹 2024-08-29 22:28:40

查看 setjs。 API 提供基本操作,并且库在设计上是不可变的。

免责声明:我是作者。

Check out setjs. The API provides basic operations and the library is immutable by design.

Disclaimer: I'm the author.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文