I have an array with objects in it, I'm trying to see if an object is included in it , but includes()
returns false, even when it's true
my array:
let borrows = [
{ bookId: 0, userId: 0 },
{ bookId: 4, userId: 1 },
{ bookId: 1, userId: 9 },
{ bookId: 2, userId: 1 },
];
the function I'm callilng:
function returning(bookId,userId){
if(!bookId | !userId){
throw new Error('parameters not supplied')
}
else{
if( borrows.includes({bookId:bookId,userId:userId})){
borrows.splice({bookId:bookId,userId:userId});
}
else{
throw new Error ('no such borrow exists')
}
}
}
calling the function:
returning(4,1)
throws error
no such borrow exists
I tried to console.log() the array and the data sent in the includes(), and it looks exactly as expected.
I ran this function separately (in the browser console) and it also returned false,
I am confident that the problem is in this line of code and would like to understand why.
I read it may be related to the address in the memory, how can I work around it without:
found=false;
for(let i =0; i<borrows.length & !found; i++){
if(borrows[i].bookId==bookId & borrows[i].userId==userId)
found=true;
}
if(found){
...
}
Thanks