2010-11-17 57 views
0

我有一个具有唯一标识的项目的主集合。 在某些时候,如果您愿意,我有一个来自主列表的属于某个子分组的ID的子集。该子集仅仅是主列表中存在的项目的ID的参考。有没有一种方法可以向主列表询问与我的子集中的ID匹配的项目,而无需遍历整个主集合? 只是试图找到最快的方式来做到这一点,而不是标准循环。在javascript中获取集合的子集的最快方法

//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 
     } 
     } 
    } 
+0

可以显示数据结构的一些例子,不仅目前您可以访问他们的方式? – Anders 2010-11-17 15:43:09

+0

有时我想知道什么最快的方式在这里... – syockit 2010-11-17 18:04:27

回答

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; 
    }; 
} 
+0

不错。我实际上希望有一个jQuery解决方案。谢谢。我会试试这个。 – topwik 2010-11-17 16:26:25

+0

有人降级它。你能说出原因吗? – 2010-11-17 19:20:38

+0

似乎是因为使用了indexOf。在IE 7中为我工作,没有自己定义indexOf。 – topwik 2010-11-17 21:35:03

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 
    } 
} 
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. 
0

你为什么要硬编码的引用,当你有语言结构?

如果你有物品的唯一ID为什么不让他们散列有效?

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

然后项目的子集可以通过三种方式来表示:

// 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 /设置操作
  • 阵列是良好的数字索引的数据时,或当最常见的用法是迭代