开发者

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

I have 2 arrays of objects. Each object has an Id property. Now, if I have a 3rd array of just Ids, what is the better and faster way of finding objects from array1 based on those Ids and moving them to array2.

Thanks a lot for answering..

Sample code:

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

array1 = ne开发者_JAVA技巧w 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?
}


If you really have 500+ objects in each array, you're probably better off using a hash to store the objects, keyed by id:

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

var people2 = {}

Now it's trivial (and much, much faster) to move things around by 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];
        }
    }
}


Good question. It actually made me go back and refer the fundamentals. The key thing about a JS array is that its sparse. You can create an array and assign values for any index (eg: 10 and 23). Based on this fact

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

NOTE: I am assuming that Person.id is an integer and less than 2^32 - 1. Refer JS documentation if id is greater or a floating point number. The JS Array implementation is not a contiguous block, so don't think assigning a value to index 12345 requires 12345 continuous blocks of memory.


Just some quick untested pseudo code. This gives a O(n^2) algorithm so it may not be best.

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


First a little function by 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);
};

Then, merging Vincent's solution

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜