簡體   English   中英

通過對象的id在javascript數組中查找和移動對象

[英]Find and move an object in javascript array by id of an objects

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

如果你真的在每個數組中有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];
        }
    }
}

好問題。 它實際上讓我回過頭來參考基本面。 關於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個連續的內存塊。

只是一些快速未經測試的偽代碼。 這給出了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++;
      }
    }
 }

首先是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++;   
    }   
  }   
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM