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

105
Views
filtering list by multiple conditions

there is a list of users

filterData = [
  {
    "position":"lawyer", 
    "department_positions":[],
    "group_positions":[
      {"group":{"id":2,"code":"234","name":"group1"},"lead":false},
      {"group":{"id":1,"code":"123","name":"group12"},"lead":true}
    ]
  },
  {
    "position":"director", 
    "department_positions":[
      {"department":{"id":3,"code":"333","name":"subDep"},"lead":false}
    ],
    "group_positions":[
      {"group":{"id":2,"code":"234","name":"group1"},"lead":false},
      {"group":{"id":1,"code":"123","name":"group12"},"lead":true}
    ]
  },
  {
    "position":"director",
    "department_positions":[],
    "group_positions":[]
  }
]

and list of filters

categories = {
  "position":["lawyer","director"],
  "group_positions":["group1","group12"],
  "department_positions":["generalDep", "subDep"]
}

It is necessary to filter users taking into account the fact that several filters can be selected at the same time. For example, i want to find user with position = "director" and AND group_positions = "group1" AND department_positions = "subDep"

my code doesn't allow filtering by multiple conditions. how can i fix it?

this.filter = this.filterData.filter(item => {
  for (let key in this.categories) {
    if (item[key].find(el => 
      this.categories[key].includes(
        el.group?.name || el.department?.name
      )
    )) {
      return true
    }
  }
  return false
})}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can try something like this:

let filtered = example.filter(item => {
  let valid = false

  if (item.includes('something')) {
    valid = true
  }

  if (!valid) {
    // check second condition
  }

  return valid
})

Use a temporary placeholder so you don't immediately have to return true/false.

about 4 years ago · Juan Pablo Isaza Report

0

This is a good place to employ an es6 class to give behavior to the object being filtered. Augment each object to determine if it matches the "category" object.

(from the example data, this assumes the OP is looking for a "product of sums" match: for all of the category keys match at least one of the category values)

class FilterMe {
  constructor(item) {
    Object.assign(this, item);
  }
  namesForKey(key) {
    switch (key) {
      case 'position':
        return [this.position]; // always answer an array
      case 'group_positions':
        return this.group_positions.map(gp => gp.group.name);
      case 'department_positions':
        return this.department_positions.map(dp => dp.department.name);
      default:
        return [];
    }
  }
  // return true if a single filter key-value pair is matched
  matchesFilterKeyValue(filterKey, filterOptions) {
    const myNames = this.namesForKey(filterKey);
    const matches = filterOptions.filter(e => myNames.includes(e));
    return matches.length > 0;
  }
  // return true if all filter key-values pairs are matched
  matchesFilter(filter) {
    return Object.entries(filter).every(keyValue => {
      return this.matchesFilterKeyValue(...keyValue);
    })
  }
}

const filterData = [{
    "position": "lawyer",
    "department_positions": [],
    "group_positions": [{
      "group": {
        "id": 2,
        "code": "234",
        "name": "group1"
      },
      "lead": false
    }, {
      "group": {
        "id": 1,
        "code": "123",
        "name": "group12"
      },
      "lead": true
    }]
  },
  {
    "position": "director",
    "department_positions": [{
      "department": {
        "id": 3,
        "code": "333",
        "name": "subDep"
      },
      "lead": false
    }],
    "group_positions": [{
      "group": {
        "id": 2,
        "code": "234",
        "name": "group1"
      },
      "lead": false
    }, {
      "group": {
        "id": 1,
        "code": "123",
        "name": "group12"
      },
      "lead": true
    }]
  },
  {
    "position": "director",
    "department_positions": [],
    "group_positions": []
  }
]

const categories = {
  "position": ["lawyer", "director"],
  "group_positions": ["group1", "group12"],
  "department_positions": ["generalDep", "subDep"]
}


// convert the filterData to the objects and test them...
let objects = filterData.map(d => new FilterMe(d));
let matches = objects.filter(o => o.matchesFilter(categories))
console.log(matches)

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!