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

221
Views
What does this JavaScript code trying to do?

I came across a block of code in a project and I find it hard to understand:

let isSelected = false;
const results = items.filter(Boolean).map(item => {
  isSelected = !isSelected && id === item.id;
  return {
    id: item.id,
    name: item.name
  };
});

What's the line isSelected = !isSelected && id === item.id; before the return is trying to do?

Thanks.

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

0

It's a confusing way of, while mapping through the array, also setting isSelected to true if any of the elements in the array match the ID.

An equivalent approach would be

const truthyItems = items.filter(Boolean);
const results = truthyItems.map(item => ({
  id: item.id,
  name: item.name
}));
const isSelected = truthyItems.some(item => item.id === id);

Side-effects inside a .map callback are usually a bit of a code smell.

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!