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

101
Vistas
js array filter based on two boolean conditions

I have two boolean conditions to filter an array. An array will always have 3 items within, and first condition always removes 2nd item, second condition 3rd one. And I successfully filtering it using .filter. But seems my approach a bit dirty, is there any better, clear way to filter?

  const firstCondition = true;
  const secondCondition = true;

  const arrayToFilter: Array<string> = ['firstItem', 'secondItem', 'thirdItem'].filter(
    (item, idx) =>
      firstCondition && secondCondition 
        ? item
        : !firstCondition && secondCondition 
        ? idx !== 1
        : !firstCondition && !secondCondition 
        ? idx === 0
        : firstCondition && !secondCondition && idx !== 2
  );

console.log(arrayToFilter);

Edit: Clarification If Conditions are false they removes items

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

0

How about this?

 const arrayToFilter: Array<string> = ['firstItem', 'secondItem', 'thirdItem'].filter(
(item, idx) => {
    if (!firstCondition && idx === 1) return false
    if (!secondCondition && idx === 2) return false
    else return true 
  }
);
about 4 years ago · Juan Pablo Isaza Denunciar

0

try,

....
(item, index) => (
  (firstCondition && index !== 1)
  || (secondCondition && index !== 2)
  || true // (!firstCondition && !secondCondition)
)
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