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