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

256
Views
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 answers
Answer question

0

Use Array.prototype.some()

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

0

You can do it like that:

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

over 4 years ago · Santiago Trujillo 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!