如何筛选不在request与friendList数组中的用户并解决重复问题
问题:筛选非好友/无好友请求用户时出现重复数据
我正在开发一个简单API,需要筛选出不在request和friendList对象数组中的用户。用map方法实现时,出现了对象被重复推入数组的问题。
相关数据
API 返回数据
[{"_id":"6304e42231ef2e7a4dec924d","email":"devrajstha@gmail.com","name":"Deepa Shrestha","phone":9819339011,"profile":"https://eduzim.co.zw/news/wp-content/uploads/2022/09/6486/pictures-former-rhythm-city-actress-pearl-petronella-tshuma-set-to-star-in-good-men-meet-the-whole-cast.jpg","dob":"2001-03-26T08:00:00.000Z","join_at":"2022-08-23T14:27:43.249Z","password":"Devraj 123@","__v":0,"friendList":[],"request":[{"_id":"63cd4c1a675ce5566d3ee6df","sender":"dstha221@gmail.com","receiver":"devrajstha@gmail.com","sendAt":"2023-01-22T14:45:20.147Z","status":"NA","__v":0}]},...]
现有JS代码
router.get("/people_y_m_n", (req, res) => { let users = []; let new_array=[]; const myEmail = 'dstha22123@gmail.com'; userModel.aggregate([ { $lookup: { from: "friend_lists", localField: "email", foreignField: "friend", as: "friendList", } }, { $lookup: { from: "frieend_requests", localField: "email", foreignField: "receiver", as: "request", } } ]).exec((err, docs) => { console.log(docs.length); docs.map((doc) => { console.log(myEmail==doc.request.sender); doc.request.map((val)=>{ if(doc.email!=val.sender||doc.email!=val.receiver && myEmail!=val.receiver || myEmail!=val.sender ){ new_array.push({ email:doc.email,name:doc.name,profile:doc.profile,_id:doc._id }) } }); doc.friendList.map((val)=>{ if(doc.email!=val.friend||doc.email!=val.me && myEmail!=val.friend || myEmail!=val.me ){ new_array.push({ email:doc.email,name:doc.name,profile:doc.profile,_id:doc._id }) } }); new_array.map((val)=>{ if(val.email!=doc.email){ users.push({ email:doc.email,name:doc.name,profile:doc.profile,_id:doc._id }) } }); }); res.send(users); }); });
当前API输出(含重复)
[{"email":"devrajstha88@gmail.com","name":"Devraj Shrestha","profile":"https://i.etsystatic.com/5713076/r/il/db3028/660348889/il_1588xN.660348889_byu0.jpg","_id":"6304e43b31ef2e7a4dec9251"},...]
new_array输出
[{"email":"devrajstha@gmail.com","name":"Deepa Shrestha","profile":"https://eduzim.co.zw/news/wp-content/uploads/2022/09/6486/pictures-former-rhythm-city-actress-pearl-petronella-tshuma-set-to-star-in-good-men-meet-the-whole-cast.jpg","_id":"6304e42231ef2e7a4dec924d"},...]
问题分析与修复
核心问题
- 逻辑判断错误:
if条件的运算符优先级和逻辑关系混乱,导致不符合条件的用户被多次推入数组。 - 滥用map方法:
map用于数组元素转换,不应用来做遍历判断,应该用forEach或直接条件判断。 - 重复添加逻辑混乱:遍历
request和friendList时,每满足一次条件就推一次用户;后续对new_array的遍历逻辑完全错误,进一步加剧重复。
修复后的代码
router.get("/people_y_m_n", (req, res) => { const myEmail = 'dstha22123@gmail.com'; userModel.aggregate([ { $lookup: { from: "friend_lists", localField: "email", foreignField: "friend", as: "friendList", } }, { $lookup: { from: "frieend_requests", localField: "email", foreignField: "receiver", as: "request", } } ]).exec((err, docs) => { if (err) { return res.status(500).send(err); } // 过滤出符合条件的用户:非好友、无相关好友请求、排除自己 const filteredUsers = docs.filter(doc => { if (doc.email === myEmail) return false; // 检查是否为好友 const isFriend = doc.friendList.some(val => (val.me === myEmail && val.friend === doc.email) || (val.me === doc.email && val.friend === myEmail) ); if (isFriend) return false; // 检查是否存在好友请求 const hasRequest = doc.request.some(val => (val.sender === myEmail && val.receiver === doc.email) || (val.sender === doc.email && val.receiver === myEmail) ); if (hasRequest) return false; return true; }).map(doc => ({ email: doc.email, name: doc.name, profile: doc.profile, _id: doc._id })); res.send(filteredUsers); }); });
修复要点
- 用filter做筛选:直接对
docs数组过滤,只保留符合条件的用户,从根源避免重复。 - 明确逻辑判断:
- 先排除当前用户自己
- 用
some方法检查是否存在好友关系,只要有一条匹配就排除 - 用
some方法检查是否存在好友请求,只要有一条匹配就排除
- 统一转换结构:过滤后用
map一次性转换为需要的输出字段,避免多次操作。 - 添加错误处理:捕获数据库查询错误,返回500状态码友好处理。
内容的提问来源于stack exchange,提问作者Debaraj Stha
相关产品推荐
相关产品推荐

