【问题标题】:fastest way to get a subset of a collection in javascript在javascript中获取集合子集的最快方法
【发布时间】:2010-11-17 15:41:17
【问题描述】:

我有一个具有唯一 ID 的项目的主集合。 在某些时候,如果您愿意,我会从主列表中获得属于某个子分组的 ID 子集。子集只是主列表中存在的项目 ID 的引用。有没有一种方法可以让我只向主列表询问与我的子集中的 ID 匹配的项目,而不必遍历整个主集合? 只是试图找到最快的方法而不是标准循环。

   //go through master list and determine which items belong to this sub item grouping
    for (var item = 0; item < masterListItems.length; ++item ) {
      for (var subItem = 0; subItem < subItems.length; ++subItem ) {
         if (masterListItems[item].Id == subItems[subItem].Id) { //if it is a sub item
           //do some UI specific thing
         }
      }
    } 

【问题讨论】:

  • 您能否展示一些数据结构的示例,而不仅仅是您当前访问它们的方式?
  • 有时我想知道这里的最快是什么意思...

标签: javascript arrays collections loops


【解决方案1】:

这是一个使用 jQuery.grep 的解决方案。过滤 3 行:

var master = [{ Id: 3 },{ Id: 1 },{ Id: 2 }]
var ids = [{ Id: 1 },{ Id: 3 }];

$(document).ready(function()
{
// Filtering with 3 lines
    idList = [];
    $.each(ids,function(index,value) { idList[idList.length] = value.Id; });
    elems = $.grep(master,function(element){ return idList.indexOf(element.Id) > -1; });

    $.each(elems,function(index,value){
        alert(value.Id);
    });
});

编辑:请注意,在 Internet Explorer 上,您必须自己定义 indexOf,如下例所示:

if(!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(needle) {
        for(var i = 0; i < this.length; i++) {
            if(this[i] === needle) {
                return i;
            }
        }
        return -1;
    };
}

【讨论】:

  • 不错。我实际上希望有一个 jquery 解决方案。谢谢。我会试试这个。
  • 有人降级了。你能说出原因吗?
  • 似乎是因为使用了indexOf。为我在 IE 7 中工作,无需自己定义 indexOf。
  • 仍然是一个很好的方法,即使对 IE6 有额外的定义,你不觉得吗?
  • 我喜欢它。这正是我想要的。
【解决方案2】:

您可以在主列表上运行一次以创建“Id”的“映射”,然后在子项目上循环一次:

var masterListMapping = new Array();
for (var i = 0; i < masterListItems.length; i++)
    masterListMapping[masterListItems[i].Id] = true;
for (var subItem = 0; subItem < subItems.length; subItem++) {
    if (masterListMapping[subItems[subItem].Id] == true) { //if it is a sub item
           //do some UI specific thing
    }
}

【讨论】:

    【解决方案3】:
    //example item is an object, ID is string
    var item = { ID: "exampleID112233",
                data: 4545 }; //sample item
    
    var masterList = {}; //masterList as a dictionary
    
    //for each item created, use its ID as its key.
    masterList["exampleID112233"] = item;
    
    var subCat1 = []; //sublist is an array of ID;
    subCat1.push("exampleID112233");
    
    //you can also make new sublists as array, push the item's ID in them.
    var subCat2 = ["anotherID334455"];
    
    //iterate through sublist
    for (var i = 0; i < subCat1.length; i++) {
      //access the referenced item
      masterList[subCat1[i]].data += 4;
    }
    
    //DELETING: remove the ID from all sublists, then delete it from masterlist.
    

    【讨论】:

      【解决方案4】:

      既然有语言结构,为什么还要硬编码引用?

      如果您有项目的唯一 ID,为什么不有效地将它们设为 散列

      // effective {hash}
      var masterListItems  = { 
        uid_1: { /* item definition */ },
        uid_2: { /* item definition */ },
        uid_3: { /* item definition */ },
        // ...
      };
      

      那么项目的子集可以用3种方式表示:

      // another hash
      var subItems = {
          uid_6:   masterListItems["uid_6"],   // effective referencing of the
          uid_321: masterListItems["uid_321"], // masterList items
          // ...
      };
      // or array of items
      var subItems = [
          masterListItems["uid_6"],
          masterListItems["uid_321"],
          // ...
      ];
      // or array of ids
      var subItems = [
          "uid_6]",
          "uid_321",
          // ...
      ];
      

      权衡:

      • 哈希 适用于唯一索引和 对许多 get/set 操作有效
      • 数组 适用于数字索引数据,或者最常见的用法是迭代

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-04
        • 1970-01-01
        • 2011-01-24
        • 2014-10-03
        • 2021-04-08
        • 2011-01-12
        相关资源
        最近更新 更多