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

146
Views
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
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!