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

202
Visualizações
Add conditional filter in JSX

What is the best way to add optional filters based on a condition? For instance, consider the following code fragment:

<Menu>
  {itemGroupLevels.map(group) = [
    this.props.itemTypes
      .filter((x) => x.group_level === group)
      .filter((x) => x.article_type_code !== "GRAL")
      {Permission.TEACHER?filter((x) => !x.article_type_code.includes('BACKPACK','LUNCH')):null}
      .map((value) => (

      ])}
</Menu>

How can I add the last filter depending on a condition such as if the user has specific permission to avoid including BACKPACK and LUNCH just if the user is a teacher:

{Permission.TEACHER?filter((x) => !x.article_type_code.includes('BACKPACK','LUNCH')):null}

Otherwise, I do not need to add the filter and just leave the two previous filters.

Thanks for your help

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Add an if condition within the filter... this ensures that if the user does not have the proper permissions the filter will not return any values.

<Menu>
  {itemGroupLevels.map(group) = [
    this.props.itemTypes
      .filter((x) => x.group_level === group)
      .filter((x) => x.article_type_code !== "GRAL")
      .filter((x) => {
       if (!Permission.TEACHER) return true
       return !x.article_type_code.includes('BACKPACK','LUNCH')
       })
      .map((value) => (

      ])}
</Menu>
about 4 years ago · Juan Pablo Isaza Relatório

0

Instead of stacking separate filter() calls, it would be better to move the business logic out into a separate function and filter once. This also means you'll avoid looping over the data multiple times for filtering.

const filterGroupItemTypes = (group, item) => {
  return (
    item.group_level === group &&
    item.article_type_code !== "GRAL" &&
    // TODO: Implement  
    (Permission.TEACHER ? ... : true)
  );
};

And then in your renderer

this.props.itemTypes.filter((item) => filterGroupItemTypes(group, item)).map(...)
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