2010-01-26 135 views
4

我有2個對象數組。每個對象都有一個Id屬性。現在,如果我有第三個只有Ids的數組,那麼根據這些Ids並將它們移動到數組2,從array1中查找對象的更好更快的方法是什麼。通過對象的id在javascript數組中查找並移動對象

非常感謝回答..

示例代碼:

Person = function(id, fn, ln) { 
    this.id = id, 
    this.firstName = fn, 
    this.lastName = ln 
} 

array1 = new Array(); 
// add 500 new Person objects to this array 

array2 = new Array(); 
// add some other new Person objects to this array 

function moveArrayItems(ids) { 
    // ids is an array of ids e.g. [1,2,3,4,5,6,...] 
    // Now I want to find all the person objects from array1 whose ids 
    // match with the ids array passed into this method. Then move them to array2. 
    // What is the best way to achive this? 
} 
+0

'... = new Array();'調用不需要。在Javascript中創建數組的最佳方式是使用數組文字:'... = [];'。 – 2010-01-26 18:38:18

回答

9

如果你真的有每個陣列中500+的對象,你可能會更好過使用散列存儲的對象,通過ID鍵:

var people = { 
       1: {id:1, name:"George Washington"}, 
       2: {id:2, name:"John Adams"}, 
       3: {id:3, name:"Thomas Jefferson"}, // ... 
      } 

var people2 = {} 

現在是微不足道的(和很多

function moveArrayItems(ids) { 
    var i,id; 
    for (i=0; i<ids.length; i++){ 
     id = ids[i]; 
     if (people1[id]) { 
      people2[id] = people1[id]; 
      delete people1[id]; 
     } 
    } 
} 
+0

我正在從json獲取objets,這是一個從o .. n開始的索引的數組。我可以通過這個循環來建立新的對象/數組與索引使用ID。除了循環之外,還有其他更好的方法嗎? – Bharat 2010-01-26 21:52:50

+0

如果您要在代碼中進行大部分操作,將數組預處理爲散列值的O(n)開銷是值得的。 – justkt 2010-01-26 22:49:52

0

只是一些未經檢驗的快速僞代碼。這給出了一個O(n^2)算法,因此它可能不是最好的

function moveArrayItems(ids) { 
    // ids is an array of ids e.g. [1,2,3,4,5,6,...] 
    //Now I want to find all the person objects from array1 whose ids match with the ids array passed into this method. Then move them to array2. 
    //What is the best way to achive this? 
    for(i = 0;i < ids.length;i++){ 
     var found = false; 
     var j = 0; 
     while(!found && j < array1.length){ 
      if(array1[j].id = ids[i]){ 
      array2.push(array1[j]); 
      found = true; 
      }    
      j++; 
     } 
    } 
} 
+0

這不會移動它們。 – 2010-01-26 18:22:04

0

首先一點功能通過John Resig

// Array Remove - By John Resig (MIT Licensed) 
Array.prototype.remove = function(from, to) { 
    var rest = this.slice((to || from) + 1 || this.length); 
    this.length = from < 0 ? this.length + from : from; 
    return this.push.apply(this, rest); 
}; 
:更快)圍繞由ID移動的東西

然後,合併文森特的解決方案

function moveArrayItems(ids) 
{ 
    // ids is an array of ids e.g. [1,2,3,4,5,6,...] 
    // Now I want to find all the person objects from array1 
    // whose ids match with the ids array passed into 
    // this method. Then move them to array2. 
    // What is the best way to achive this? 

    for(i = 0; i < ids.length; i++) 
    {  
    var found = false; 
    var j = 0; 
    while(!found && j < array1.length) 
    { 
     if(array1[j].id = ids[i]) 
     { 
     array2.push(array1[j]); 
     array1.remove(i); 
     found = true; 
     }     
     j++; 
    } 
    } 
} 
1

好問題。這實際上讓我回去參考基礎知識。關於JS陣列的關鍵是它的稀疏。您可以創建一個數組併爲任何索引分配值(例如:10和23)。基於這一事實

array1 = new Array(); 

array1[person1.id] = person1; 
array1[person2.id] = person2; 
.... goes till N 

function moveArrayItems(ids) { 
    for(index in ids) { 
     array2.push(array1[ids[index]]); 
     delete array1[ids[index]]; 
    } 
} 

注:我假設Person.id是整數並且小於2^32 - 1.參見JS文件,當ID是大於或浮點數。 JS Array實現不是連續的塊,所以不要認爲爲索引12345分配值需要12345個連續的內存塊。

+0

這與三聯在速度方面的答案類似。我認爲是因爲javascript對象就像散列表,你可以指定成爲該條目關鍵字的字段名稱。我想和你確認一下,以確保速度與其他答案相同。 – Bharat 2010-01-26 21:50:17

+0

如果兩者都以相同的速度運行,我不會感到驚訝。 JS數組也像散列表,除了鍵必須是非負整數。使用數組可能比普通對象稍快。瀏覽器知道它的一個數組,並可能爲迭代優化它。在對象技術中,它是一個瀏覽器的對象和一個哈希表。你可以做一個秒錶類型分析的答案和決定。如果您可以等待一段時間,我會嘗試做一些分析並將其發佈到此處。 – 2010-01-26 22:38:02

相關問題