2010-11-17 54 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 /設置操作
  • 陣列是良好的數字索引的數據時,或當最常見的用法是迭代