在Javascript中是否有用于Set数据类型的库?

时间:2022-08-06 16:26:34

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?

既然Javascript没有内置的set数据类型,那么任何人都会遇到一个像集合和设置操作(如union,intersection等)的体面库?

5 个解决方案

#1


19  

Have a look at JS.Set.

看看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.

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

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

它包含联合,交集,合并等方法......

#2


7  

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.

如果您只想访问简单的union,intersection函数,还可以尝试使用Underscore.js的内置Array函数。它还为数据操作提供了许多更有用的实用程序,因此如果没有,请尝试使用它。

#3


3  

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

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

Disclaimer: I'm the author.

免责声明:我是作者。

#4


3  

Sets are now native in ES2015.

套件现在在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.

这适用于使用babel进行转换或仅使用firefox中的本地进行转换。

#5


1  

immutable-js expose a powerfull set data-structure.

immutable-js暴露了一个强大的集合数据结构。

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

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

#1


19  

Have a look at JS.Set.

看看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.

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

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

它包含联合,交集,合并等方法......

#2


7  

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.

如果您只想访问简单的union,intersection函数,还可以尝试使用Underscore.js的内置Array函数。它还为数据操作提供了许多更有用的实用程序,因此如果没有,请尝试使用它。

#3


3  

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

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

Disclaimer: I'm the author.

免责声明:我是作者。

#4


3  

Sets are now native in ES2015.

套件现在在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.

这适用于使用babel进行转换或仅使用firefox中的本地进行转换。

#5


1  

immutable-js expose a powerfull set data-structure.

immutable-js暴露了一个强大的集合数据结构。

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

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