Ran into this for the first time today.
const needle = {key1: 'value1'}
const haystack = [{key1: 'value1'}, {key2: 'value2'}]
const idx1 = haystack.findIndex((hay) => hay.key1 === needle.key1);
const idx2 = haystack.findIndex((hay) => hay.key2 === needle.key1);
const idx3 = haystack.findIndex((hay) => hay.id === needle.id);
const idx4 = haystack.findIndex((hay) => undefined === undefined);
console.log(idx1, idx2, idx3, idx4)
// 0 -1 0 0
// ideally would return
// 0 -1 -1 0
Is there a clever way to get findIndex to return -1 in this case or do I have to put guards around it?
Thanks @Xupitan. I like your solution.
const idx = haystack.findIndex((hay) => hay.id && hay.id === needle.id);
console.log(idx)
// -1