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

106
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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