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

149
Vistas
Checking for the existence of an element in array full of objects

I have an array of likes which includes the ids of the users that have liked the post (this is originally a mongoose model). I want to check if the user's id is in the array.

const likes =  
[
    {
        "user": "6283986e931a243f64eb063e"
    },
    {
        "user": "6283986e931a243f64eb0123s"
    },
]

const user = "6283986e931a243f64eb063e";

const hasBeenLiked = Object.values(likes).includes(user);

console.log(hasBeenLiked) //returns false

Obviously i cannot check this way. I am aware that i can do this with loop, but i want more lightweight approach (if possible).

The orignal code is:

const hasBeenLiked = Object.values(article.likes).includes(req.headers.user);

If this is not possible without the use of loops is there a mongoose command for this?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Use Array.prototype.some()

const hasBeenLiked = likes.some(like => like.user === user);
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can do it like that:

const hasBeenLiked = !!likes.find(l => l.user == user)

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