J'ai 2 tableaux d'objets. Chaque objet a une propriété Id. Maintenant, si j'ai un troisième tableau de juste IDs, quelle est la meilleure et plus rapide façon de trouver des objets de array1 en fonction de ces Ids et les déplaçant vers array2.Trouver et déplacer un objet dans un tableau javascript par l'ID d'un objet
Merci beaucoup pour répondre ..
Exemple de code:
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?
}
Les appels '... = new Array();' ne sont pas nécessaires. La meilleure façon de créer des tableaux en Javascript est d'utiliser le tableau littéral: '... = [];'. –