<small id='EErmy'></small><noframes id='EErmy'>

      <tfoot id='EErmy'></tfoot>
      • <bdo id='EErmy'></bdo><ul id='EErmy'></ul>

      <i id='EErmy'><tr id='EErmy'><dt id='EErmy'><q id='EErmy'><span id='EErmy'><b id='EErmy'><form id='EErmy'><ins id='EErmy'></ins><ul id='EErmy'></ul><sub id='EErmy'></sub></form><legend id='EErmy'></legend><bdo id='EErmy'><pre id='EErmy'><center id='EErmy'></center></pre></bdo></b><th id='EErmy'></th></span></q></dt></tr></i><div id='EErmy'><tfoot id='EErmy'></tfoot><dl id='EErmy'><fieldset id='EErmy'></fieldset></dl></div>

        <legend id='EErmy'><style id='EErmy'><dir id='EErmy'><q id='EErmy'></q></dir></style></legend>

        JavaScript 中是否有类似 Java Set 的数据结构?

        Is there a data structure like the Java Set in JavaScript?(JavaScript 中是否有类似 Java Set 的数据结构?)

          <legend id='tWpMs'><style id='tWpMs'><dir id='tWpMs'><q id='tWpMs'></q></dir></style></legend>

              <tbody id='tWpMs'></tbody>

          • <tfoot id='tWpMs'></tfoot>

              <i id='tWpMs'><tr id='tWpMs'><dt id='tWpMs'><q id='tWpMs'><span id='tWpMs'><b id='tWpMs'><form id='tWpMs'><ins id='tWpMs'></ins><ul id='tWpMs'></ul><sub id='tWpMs'></sub></form><legend id='tWpMs'></legend><bdo id='tWpMs'><pre id='tWpMs'><center id='tWpMs'></center></pre></bdo></b><th id='tWpMs'></th></span></q></dt></tr></i><div id='tWpMs'><tfoot id='tWpMs'></tfoot><dl id='tWpMs'><fieldset id='tWpMs'></fieldset></dl></div>

                <small id='tWpMs'></small><noframes id='tWpMs'>

                  <bdo id='tWpMs'></bdo><ul id='tWpMs'></ul>

                • 本文介绍了JavaScript 中是否有类似 Java Set 的数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想在 JavaScript 中使用可用于存储 ID 数量的数据结构.我应该能够检查该集合中是否已经存在某个键,例如 Java 集合.

                  I want to use a data structure in JavaScript that can be used to store number of IDs. I should be able to check if a key already exists in that set, something like Java Sets.

                  我想实现以下相同的行为(此代码是 Java 代码):

                  I want to achive same behaviours as follows (this code is in Java):

                  Set<String> st = new HashSet<String>();
                  //add elemets
                  
                  if(st.contains("aks") ){
                    //do something
                  }
                  

                  我想要一个与上述代码等效的 JavaScript/dojo.

                  I want a JavaScript/dojo equivalent of the above code.

                  推荐答案

                  我已经编写了一个 JavaScript HashSet 实现,它可以做你想做的事情并允许任何对象成为集合的成员:http://code.google.com/p/jshashtable

                  I've written a JavaScript HashSet implementation that does what you want and allows any object to be a member of the set: http://code.google.com/p/jshashtable

                  但是,如果您只需要存储字符串,您可以通过将集合成员存储为普通对象的属性名称来做一些更简单的事情.例如:

                  However, if you just need to store strings, you could do something more simply by storing set members as property names of a normal Object. For example:

                  function StringSet() {
                      var setObj = {}, val = {};
                  
                      this.add = function(str) {
                          setObj[str] = val;
                      };
                  
                      this.contains = function(str) {
                          return setObj[str] === val;
                      };
                  
                      this.remove = function(str) {
                          delete setObj[str];
                      };
                  
                      this.values = function() {
                          var values = [];
                          for (var i in setObj) {
                              if (setObj[i] === val) {
                                  values.push(i);
                              }
                          }
                          return values;
                      };
                  }
                  

                  关于实现的说明:valStringSet 实现内部使用的对象,每个集合都是唯一的.将属性名称构成集合 (setObj) 的对象的属性值与 val 进行比较,无需进行 hasOwnProperty() 检查和保证只有添加到集合中的字符串才会显示在 values 中.

                  A note about the implementation: val is an object used internally by the StringSet implementation that is unique to each set. Comparing property values of the object whose property names make up the set (setObj) against val eliminates the need for a hasOwnProperty() check and guarantees that only strings that have been added to the set will show up in values.

                  示例用法:

                  var set = new StringSet();
                  set.add("foo");
                  set.add("bar");
                  
                  alert(set.contains("foo")); // true
                  alert(set.contains("baz")); // false
                  
                  set.values(); // ["foo", "bar"], though not necessarily in that order
                  set.remove("foo");
                  set.values(); // ["bar"]
                  

                  这篇关于JavaScript 中是否有类似 Java Set 的数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Compiling C++ for the JVM(为 JVM 编译 C++)
                  Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                  How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                  Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                  How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                  Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)

                      <bdo id='LcMBB'></bdo><ul id='LcMBB'></ul>
                    • <tfoot id='LcMBB'></tfoot>
                      <legend id='LcMBB'><style id='LcMBB'><dir id='LcMBB'><q id='LcMBB'></q></dir></style></legend>

                        <i id='LcMBB'><tr id='LcMBB'><dt id='LcMBB'><q id='LcMBB'><span id='LcMBB'><b id='LcMBB'><form id='LcMBB'><ins id='LcMBB'></ins><ul id='LcMBB'></ul><sub id='LcMBB'></sub></form><legend id='LcMBB'></legend><bdo id='LcMBB'><pre id='LcMBB'><center id='LcMBB'></center></pre></bdo></b><th id='LcMBB'></th></span></q></dt></tr></i><div id='LcMBB'><tfoot id='LcMBB'></tfoot><dl id='LcMBB'><fieldset id='LcMBB'></fieldset></dl></div>
                          <tbody id='LcMBB'></tbody>

                          <small id='LcMBB'></small><noframes id='LcMBB'>