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

99
Visualizações
The best way to filter down a dataSet with javascript

The idea was to query a dataset with querystring params. I only want the "records" to match only what was queried.

Dataset

{
 1111:
 {
 Category: "Education"
 Role: "Analyst"
 }
 2222:
 {
 Category: "Communications and Media"
 Role: "Analyst"
 }
 3333:
 {
 Category: "Public Sector"
 Role: "Something else"
 }
 4444:
 {
 Category: "Public Sector"
 Role: "Something else"
 }
...
}
[[Prototype]]: Object

I'm sending in qString

Category: (2) ['Communications and Media', 'Education']
Role: ['Analyst']
length: 0
[[Prototype]]: Array(0)

I'd like to loop over that and filter/reduce so I only have records that match. Sort of an and instead of an or.

dataSet is an Object of objects. Thoughts? Thanks in advance.

export const Filtered = (qStrings, dataSet) => {
  const filtered = [];
  Object.entries(qStrings).forEach(([field]) => {
    qStrings[field].forEach((value) => {
      filtered.push(
        ..._.filter(dataSet, (sess) => {
          if (sess[field] && sess[field].toString() === value.toString()) {
            return sess;
          }
        })
      );
    });
  });
  return _.uniq(filtered);
};
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

geez, I figured it out with a colleague who's way smarter than me wink Jess!

export const Filtered = (qStrings, dataSet) => {
  let filtered = [];

  Object.entries(qStrings).forEach(([field], idx) => {
    let source = filtered;
    if (idx === 0) {
      source = dataSet;
    }
    filtered = _.filter(source, (sess) => {
      return sess[field] && sess[field].includes(qStrings[field]);
    });
  });
  return _.uniq(filtered);
};

Now to clean this up.

about 4 years ago · Juan Pablo Isaza Relatório

0

Not sure if this solves your problem exactly, but you can apply this logic without mutation for a much cleaner function.

export const matches = (qStrings, dataSet) =>
  Object.entries(dataSet).reduce((acc, [key, value]) =>
    Object.entries(value).every(([rKey, rValue]) => qStrings[rKey]?.includes(rValue))
      ? { ...acc, [key]: value }
      : acc,
    {});

This will return records 1111 and 2222 because they match one of the categories and the role in qStrings.

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