Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

258
Visualizações
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?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Use Array.prototype.some()

const hasBeenLiked = likes.some(like => like.user === user);
over 4 years ago · Santiago Trujillo Relatório

0

You can do it like that:

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

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda