Me encontré con esto por primera vez hoy.
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 ¿Hay alguna forma inteligente de hacer que findIndex devuelva -1 en este caso o tengo que poner guardias a su alrededor?
Gracias @Xupitan. Me gusta tu solución.
const idx = haystack.findIndex((hay) => hay.id && hay.id === needle.id); console.log(idx) // -1