Is there a library for a Set data type in Javascript?

前端 未结 6 1525
迷失自我
迷失自我 2020-12-09 09:28

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?

相关标签:
6条回答
  • 2020-12-09 10:14

    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.

    0 讨论(0)
  • 2020-12-09 10:15

    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.

    0 讨论(0)
  • 2020-12-09 10:18

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

    Disclaimer: I'm the author.

    0 讨论(0)
  • 2020-12-09 10:23

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

    0 讨论(0)
  • 2020-12-09 10:24

    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.

    0 讨论(0)
  • 2020-12-09 10:27

    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))
    
    0 讨论(0)
提交回复
热议问题