Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

101
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda