2010-01-26 82 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