我有2個對象數組。每個對象都有一個Id屬性。現在,如果我有第三個只有Ids的數組,那麼根據這些Ids並將它們移動到數組2,從array1中查找對象的更好更快的方法是什麼。通過對象的id在javascript數組中查找並移動對象
非常感謝回答..
示例代碼:
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?
}
'... = new Array();'調用不需要。在Javascript中創建數組的最佳方式是使用數組文字:'... = [];'。 – 2010-01-26 18:38:18