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

148
Vistas
How to check if an id-String is included in an array of mongoose ObjectIds?

My code is somewhat inconsistent since I have one array of ObjectIds an one array of Ids as Strings.

array1 = [new ObjectId("2"), new ObjectId("3"), new ObjectId("4")]

array2 = ["1","4"]

I want to iterate over array2 and check, if the Id is included in array1 (using "includes", I could loop inside loop but that's what I try to avoid). Unfortunately "includes" seems to be Type-sensitive.

array2.map(id => {array1.inclundes(id) && console.log("is included!"})

does not work as well as:

array2.map(id => {array1.includes(mongoose.Tyes.ObjectId(id)) && console.log("is included"})

which I thought would convert the "id" in the proper Type for comparison.

What works is:

array2.map(id2 => {
    array1.map(id1 => {
        if(id1 == id2){
            console.log("included")
        }
    })
})

While the following also don't work:

array2.map(id2 => {
    array1.map(id1 => {
        if(id1 === id2){
            console.log("included")
        }
    })
})

So I think there is a Type-related Error even if both are "Object" after mongoose.Types.ObjectId(id)

about 4 years ago · Juan Pablo Isaza
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