Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

105
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda