Set data structure of Java in javascript/jQuery

后端 未结 5 1769
臣服心动
臣服心动 2021-02-02 10:52

Is there any way to create Set data structure(Unique Collections) like java in javascript?

5条回答
  •  南旧
    南旧 (楼主)
    2021-02-02 11:20

    For a set of strings, I would just use a object with the value true.

    var obj = {};
    obj["foo"] = true;
    obj["bar"] = true;
    
    if(obj["foo"])
    {
      // foo in set
    }
    

    This is basically how HashSet works in Java, assuming the JavaScript object is implemented as a hashtable (which is typical).

提交回复
热议问题