Finding and moving an object in a javascript array by object id

I have 2 arrays of objects. Each object has an Id property. Now, if I have a 3rd array of identifiers only, what is the best and fastest way to search for objects from array1 based on these identifiers and move them to array2.

Thank you very much for your reply.

Code example:

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 eg [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
source share
4 answers

If you really have 500 objects in each array, you are probably better off using a hash to store the objects, with the key 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 is trivial (and much, much faster) to move things 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]; } } } 
+9
source
Good question. It actually made me go back and talk about the basic principles. The main thing in the JS array is sparse . You can create an array and assign values ​​for any index (for example: 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 assume that Person.id is an integer and less than 2 ^ 32 - 1. Refer to the JS documentation if the identifier is greater than or a floating point number. The JS Array implementation is not an adjacent block, so don't think that assigning a value to index 12345 requires 12345 contiguous blocks of memory.

+1
source

Just a quick untested pseudo code. This gives the O (n ^ 2) algorithm, so it couldn't be better .

  function moveArrayItems(ids) { // ids is an array of ids eg [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
source

First a little John Resig feature

 // 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 eg [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
source

Source: https://habr.com/ru/post/1299458/


All Articles