let database = [{
name: 'James Bond',
code: '007'
},
{
name: 'El',
code: '11'
}
]
let subject = {
name: 'James Bond',
code: '007'
}
database.indexOf(subject)
This returns -1. I found similar questions here on SO but they were all mostly asking to find index of the object by comparing it to a key value pair.
database[0]andsubjectare different objects, even if they have the same values. They point to different memory addresses. You do need to compare the values to see if the objects look the same. Alternatively, you can do what you want if you haddatabase[0] = subjectand thendatabase.indexOf(subject)will be correct.