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

220
Vistas
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 Respuestas
Responde la pregunta

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