Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda