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

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

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 Report

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