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

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

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 Report

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