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

前端 未结 6 1534
迷失自我
迷失自我 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: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))
    

提交回复
热议问题