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

101
Views
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 answers
Answer question

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 Report

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 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!