Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

100
Vistas
Recoil Tutorial findIndex

In the Recoil tutorial at https://recoiljs.org/docs/basic-tutorial/atoms, they use
const index = todoList.findIndex((listItem) => listItem === item); to get the index of an object in an array. The code works but I can't replicate it, I always get a return value of -1. I thought you couldn't compare objects directly like that?
The todoList is something like:

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

where item is {id: 0, text: 'item1', isComplete: false}

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

They are used logic like this:

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

if you defined list by your own:

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

you should use id || text || isComplete to find index, because you don't have a link to the object that you want to find. More about object comparision https://dmitripavlutin.com/how-to-compare-objects-in-javascript/

about 4 years ago · Juan Pablo Isaza Denunciar

0

You're imagining that the === is comparing the object by value, when in fact it is comparing them by reference, see below.

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda