通过对象的id在javascript数组中查找和移动对象

时间:2010-01-26 18:11:45

标签: javascript jquery

我有2个对象数组。每个对象都有一个Id属性。现在,如果我有一个只有Ids的第三个数组,那么基于这些ID并将它们移动到array2,从array1中查找对象的更好更快的方法是什么。

非常感谢您回答..

示例代码:

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?
}

4 个答案:

答案 0 :(得分: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 = {}

现在通过ID移动事物是微不足道的(并且更快,更快):

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];
        }
    }
}

答案 1 :(得分: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.如果id更大或浮点数,请参阅JS文档。 JS Array实现不是连续的块,因此不要认为为索引12345分配值需要12345个连续的内存块。

答案 2 :(得分: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++;
      }
    }
 }

答案 3 :(得分: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);
};

然后,合并文森特的解决方案

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