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

228
Views
Unable to delete object in an array in JavaScript

I have been trying to delete an object with a specific value from an array and all code and syntax seems abortive even with the ones I copied on this platform.

// UNABLE TO WRITE "DELETE" CAMPUS DIRECTOR CODE
router.delete("/:id/director", userAuth, checkRole(["admin"]), async(req, res) => {
    const campus = await Campus.findById(req.params.id);
    const campusAdmins = campus.admins
    // campusAdmin will return a single object in an array [{_id: 1234567865434543, title: "campus director", userId: "9876543456fdfgfdgf"}]
    try {
        campusAdmins.splice(campusAdmins.findIndex(e => e.title === "campus director"),1);
        console.log(campusAdmins);
        res.status(201).json("You've successfully delete campus director");
    } catch (err) {
        res.status(500).json(err);
    }
})

help me solve this problem pls.

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

0

Are you sure campusAdmins returns the object you've specified in the comment?

There is no issue with the syntax, this works:

const campusAdmins = [{
  _id: 1234567865434543,
  title: "campus director",
  userId: "9876543456fdfgfdgf"
}];

try {
  campusAdmins.splice(campusAdmins.findIndex(e => e.title === "campus director"), 1);
  console.log(campusAdmins);
  console.log("Successfully deleted campus director");
} catch (err) {
  console.log("Failure");
}

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!