我有两个对象数组。每个对象都有一个Id属性。现在,如果我有一个只包含Ids的第三个数组,找到基于这些Ids从array1中的对象并将它们移动到array2的更好和更快的方法是什么。
感谢您的回答。
示例代码:
感谢您的回答。
示例代码:
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中创建数组的最佳方法是使用数组字面量:... = [];
。 - Matt Ball