Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

79
Views
.filter() not filtering the items from the array

I have this array of data in Javascript:

// console.log(currentMembers.members)
// *************************
// CURRENT MEMBERS
// *************************
// [
//   new ObjectId("62385d8caee17d13a1762b39"),
//   new ObjectId("6238a480170aff10d16ccd86"),
//   new ObjectId("6238a480170aff10d16ccd86"),
//   new ObjectId("6238a608170aff10d16ccd89")
// ]

I want to remove from the array one value that matches the variable "memberToRemove". So .filter() should be enough to perform this but it doesn't and I'm lost.

try {
    const newListofMembers = currentMembers.members.filter(
      member => member._id !== memberToRemove
    );
const updatedMembers = await Group.findOneAndUpdate(
    { _id: groupId },
    { members: newListofMembers }
    );
    console.log('Users successfully updated.');
    return res.status(200).json({ success: true, members: newListofMembers });
  } catch (err) {
    console.log(err);
    next(err);
  }

When I perform this action nothing happens, when I console.log(newListOfMembers) the filter doesn't seem to work at all, it ignores the member => member !== memberToRemove. The member to remove is 6238a608170aff10d16ccd89.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Simplest, you can change you filter into this member => String(member._id) === memberToRemove

But the right way is

const _ = require('lodash');
const { Types } = require('mongoose');

const newListofMembers = currentMembers.members.filter(
      member => !_.isEqual(member._id, Types.ObjectId(memberToRemove))
);

You can replace member => !_.isEqual(member._id, Types.ObjectId(memberToRemove)) with member => !_.isEqual(member, Types.ObjectId(memberToRemove)) or !item.equals(memberToRemove)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!