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)