Javascript 获取唯一的ObjectID';从包含重复项的数组中删除
所以我有一个ObjectID数组,例如:Javascript 获取唯一的ObjectID';从包含重复项的数组中删除,javascript,mongoose,Javascript,Mongoose,所以我有一个ObjectID数组,例如: console.log(objectIdArray); 给出[ObjectID,ObjectID,ObjectID] 但是,此处存在重复项,如映射到ID字符串时所示: var idArray = objectIdArray.map(objectId => objectId.toString()); console.log(idArray); 给出[“5afa54e5516c5b57c0d43227”、“5afa54e5516c5b57c0d43
console.log(objectIdArray);
给出[ObjectID,ObjectID,ObjectID]
但是,此处存在重复项,如映射到ID字符串时所示:
var idArray = objectIdArray.map(objectId => objectId.toString());
console.log(idArray);
给出[“5afa54e5516c5b57c0d43227”、“5afa54e5516c5b57c0d43227”、“5afa54f0516c5b57c0d43228”
,您可以看到以27结尾的ID重复了两次
如何筛选ObjectID数组以删除重复项(保留完整的ObjectID对象,而不仅仅是ID字符串值)?如果使用ES6,您可以接受Sajeetharan的答案,但创建一组对象,而不是其ID:
let nodupes = [...new Set(objectIdArray)];
此解决方案将删除不是第一个具有特定Id的对象的所有对象
我们用id填充数组
,然后检查当前列表中是否已经填充了id
如果存在大量元素,则上述解决方案可能会很慢,因为您需要在
inputArray
中的每次迭代中检查ID列表,该列表为O(n),这将使算法处于O(n^2)+O(n)
因此,我们可以先根据toString()
对其进行排序,然后验证当前id是否与我们看到的上一个id不匹配
const removeDuplicates = inputArray => {
const sortedArray = inputArray.sort((a,b) => (a.toString() > b.toString() ? 1 : (a.toString() < b.toString() ? -1 : 0)));
let lastSeen = undefined;
return sortedArray.reduce((sum, element) => {
if(lastSeen !== element.toString()){
sum.push(element);
}
lastSeen = element.toString();
return sum;
}, []);
};
const removeDuplicates=inputArray=>{
const-sortedArray=inputArray.sort((a,b)=>(a.toString()>b.toString()?1:(a.toString(){
if(lastSeen!==element.toString()){
和推(元素);
}
lastSeen=element.toString();
回报金额;
}, []);
};
现在算法是O(n logn)+O(n),假设sort使用我建议使用MongoDB聚合管道,以防止最终结果中出现重复ObjectId值的情况 然而:
// Define callback Function to receive modified Array
var receiveModifiedArray = function(objectIdArray) {
// log modified Array to console
console.log(objectIdArray);
}
// Remove duplicate ObjectId values
function removeDuplicateObjectIdValues(objectIdArray, callback) {
// Iterate through each ObjectId
objectIdArray.forEach((currentValue, index, array) => {
// evaluate Array items with index greater than 0
if(index > 0) {
// check ObjectId string values for type and value equality
if(currentValue.toString() == array[index -1].toString()) {
/**
* The splice() method changes the contents of an array
* by removing existing elements and/or adding new elements.
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
*/
objectIdArray.splice(index,1);
}
// If processing last item in Array, callback
if(index == array.length +1) {
callback(objectIdArray);
}
}
});
// Return to move on to next message in call stack
return;
}
// Remove duplicate ObjectId values
removeDuplicateObjectIdValues(objectIdArray,receiveModifiedArray);
它们可以处理所有事情:我不相信JavaScript集可以处理完整对象。执行上面提到的操作不会删除任何ObjectID。这是因为对象不按其内容进行比较,它们必须是对同一对象的引用。我经常将数组的值添加到映射中(
var map={};array.forEach(id=>map[id]=true//或原始对象)
),然后返回object.keys(map)
或对象值(映射)
。O(n)时间。
// Define callback Function to receive modified Array
var receiveModifiedArray = function(objectIdArray) {
// log modified Array to console
console.log(objectIdArray);
}
// Remove duplicate ObjectId values
function removeDuplicateObjectIdValues(objectIdArray, callback) {
// Iterate through each ObjectId
objectIdArray.forEach((currentValue, index, array) => {
// evaluate Array items with index greater than 0
if(index > 0) {
// check ObjectId string values for type and value equality
if(currentValue.toString() == array[index -1].toString()) {
/**
* The splice() method changes the contents of an array
* by removing existing elements and/or adding new elements.
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
*/
objectIdArray.splice(index,1);
}
// If processing last item in Array, callback
if(index == array.length +1) {
callback(objectIdArray);
}
}
});
// Return to move on to next message in call stack
return;
}
// Remove duplicate ObjectId values
removeDuplicateObjectIdValues(objectIdArray,receiveModifiedArray);