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

102
Views
Tutorial de retroceso findIndex

En el tutorial de Recoil en https://recoiljs.org/docs/basic-tutorial/atoms , usan
const index = todoList.findIndex((listItem) => listItem === item); para obtener el índice de un objeto en una matriz. El código funciona pero no puedo replicarlo, siempre obtengo un valor de retorno de -1. ¿Pensé que no podías comparar objetos directamente así?
La todoList es algo así como:

 [{id: 0, text: 'item1', isComplete: false}, {id: 1, text: 'item2', isComplete: false}]

donde item es {id: 0, text: 'item1', isComplete: false}

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

0

Se utilizan lógicas como esta:

 const a = {}; const b = []; b.push(a); b.findIndex(el=>el===a); // 0

si definió la lista por su cuenta:

 [{id: 0, text: 'item1', isComplete: false}, {id: 1, text: 'item2', isComplete: false}]

deberías usar id || text || isComplete para encontrar el índice, porque no tiene un enlace al objeto que desea buscar. Más sobre la comparación de objetos https://dmitripavlutin.com/how-to-compare-objects-in-javascript/

about 4 years ago · Juan Pablo Isaza Report

0

Está imaginando que === está comparando el objeto por valor , cuando en realidad los está comparando por referencia , vea a continuación.

 const todoList = []; const item = {id: 0, text: 'item1', isComplete: false}; todoList.push(item); // Attempt by reference, 0 console.log(todoList.findIndex((listItem) => listItem === item)); // Attempt by value, -1 console.log(todoList.findIndex((listItem) => listItem === {id: 0, text: 'item1', isComplete: false}));

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!