在javascript中获取集合子集的最快方法

时间:2010-11-17 15:41:17

标签: javascript arrays collections loops

我有一个具有唯一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
         }
      }
    } 

4 个答案:

答案 0 :(得分:0)

您可以在主列表上运行一次以创建“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
    }
}

答案 1 :(得分:0)

这是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;
    };
}

答案 2 :(得分:0)

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

答案 3 :(得分:0)

为什么在有语言结构的时候想要硬编码引用呢?

如果您对商品有唯一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",
    // ...
];

权衡:

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