Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

269
Views
¿Cómo evitar que findIndex devuelva 0 cuando los valores no están definidos?

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Gracias @Xupitan. Me gusta tu solución.

 const idx = haystack.findIndex((hay) => hay.id && hay.id === needle.id); console.log(idx) // -1
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!