根据对象的id在javascript数组中查找并移动对象

根据对象的id在javascript数组中查找并移动对象,javascript,jquery,Javascript,Jquery,我有两个对象数组。每个对象都有一个Id属性。现在,如果我有第三个仅包含ID的数组,那么基于这些ID从array1查找对象并将其移动到array2的更好更快的方法是什么 非常感谢你的回答 示例代码: Person = function(id, fn, ln) { this.id = id, this.firstName = fn, this.lastName = ln } array1 = new Array(); // add 500 new Person objects to t

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

非常感谢你的回答

示例代码:

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

只是一些快速的未经测试的伪代码。这给出了一个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++;
      }
    }
 }
函数moveArrayItems(ID){
//ids是一个ID数组,例如[1,2,3,4,5,6,…]
//现在,我想找到array1中ID与传入此方法的ID数组匹配的所有person对象,然后将它们移动到array2。
//实现这一目标的最佳方式是什么?
对于(i=0;i
如果每个数组中确实有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];
        }
    }
}
函数moveArrayItems(ID){
变量i,id;

对于(i=0;i首先通过

//数组删除-由John Resig(麻省理工学院授权)
Array.prototype.remove=函数(从,到){
var rest=this.slice((to | | from)+1 | | this.length);
this.length=from<0?this.length+from:from;
返回此。推。应用(此,其余);
};
然后,合并文森特的解决方案

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++;   
    }   
  }   
}
函数moveArrayItems(ID)
{   
//ids是一个ID数组,例如[1,2,3,4,5,6,…]
//现在我想找到array1中的所有person对象
//其ID与传入的ID数组匹配
//此方法。然后将它们移动到阵列2。
//实现这一目标的最佳方式是什么?
对于(i=0;i
问得好。这实际上让我回到了基础知识。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数组实现不是一个连续的块,所以不要认为为索引12345赋值需要12345个连续的内存块。

The
…=new Array();
不需要调用。在Javascript中创建数组的最佳方法是使用数组文本:
…=[]
。这与Triptych的答案在速度方面类似吗。我认为是这样,因为javascript对象类似于哈希表,您可以指定字段名,该字段名将成为该条目的键。我想与您确认一下,以确保速度与其他答案相同。如果两者以相同的速度运行,我不会感到惊讶。JS数组也是如此ke hashtable(除键外)必须是非负整数。使用数组可能比普通对象稍快。浏览器知道它是一个数组,并可能为迭代对其进行优化。在对象技术中,它是浏览器的一个对象,是您的一个哈希表。您可以对答案和Decision进行秒表类型分析。如果您可以在一段时间内,我将尝试进行一些分析,并将其发布在这里。我从json获取对象,这是一个从o..n获取索引的数组。我可以循环使用此方法,并使用id构建具有索引的新对象/数组。除了循环之外,还有其他更好的方法吗?如果您打算在代码中执行大部分操作,o(n)将数组预处理为哈希的成本是值得的。
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]];
  }
}