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

111
Views
How to conditionally filter the data

I'm writing a small js code where I need to filter the data based on key passed. Here, the main issue is, the data is not consistent(please refer to my code sample).

var users = [{
   name: 'paul',
   job: 'engineer'
},
{
    name: 'John',
    job: 'Mechanic'
},
{
    name: 'paul',
    job: 'Mechanic'
},
{
    name: 'George',
    job: 'Plumber'
},
{
    name: 'John'
},
];

filtersToApply = {
  job: 'engineer'
};




returnFilteredList = (users, columnDataToFilter) => {
  return users.filter((row) => {
    return Object.keys(columnDataToFilter).every(
      (propertyName) =>
      row[propertyName]
      .toString()
      .toLowerCase()
.indexOf(columnDataToFilter[propertyName].toString().toLowerCase()) >
      -1
    );
  });
};

console.log(JSON.stringify(returnFilteredList(users, filtersToApply)));

Here I get the error, 'coz, there is no job for the last JSON object in the array. how can I handle this?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could get the entries of you filter conditions and check with Array#every or Array#some, depending on the need.

const
    users = [{ name: 'paul', job: 'engineer' }, { name: 'John', job: 'Mechanic' }, { name: 'paul', job: 'Mechanic' }, { name: 'George', job: 'Plumber' }, { name: 'John' }],
    filtersToApply = { job: 'engineer' },
    filters = Object.entries(filtersToApply),
    result = users.filter(user =>
        filters.every(([k, v]) => (user[k] || '').toLowerCase() === v)
    );

console.log(result);

If you have an array or only a sting, you need to compare each value and adjust the case in advance.

const
    users = [{ name: 'paul', job: 'engineer' }, { name: 'John', job: ['Mechanic', 'Engineer'] }, { name: 'paul', job: 'Mechanic' }, { name: 'George', job: 'Plumber' }, { name: 'John' }],
    filtersToApply = { job: 'engineer' },
    filters = Object.entries(filtersToApply),
    result = users.filter(user =>
        filters.every(([k, v]) => []
            .concat(user[k] || [])
            .map(s => s.toLowerCase())
            .includes(v)
        )
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

Sometimes row[propertyName] will be undefined. You can use an Optional Chaining operator to avoid the errors:

return Object.keys(columnDataToFilter).every(
      (propertyName) =>
      row[propertyName]? //<--
      .toString()
      .toLowerCase()
      .indexOf(columnDataToFilter[propertyName].toString().toLowerCase()) >
      -1
    );
about 4 years ago · Juan Pablo Isaza Report

0

return Object.keys(columnDataToFilter).every(
    (propertyName) =>
    row[propertyName]|| '' //you can add undefined keys to empty string.
    .toString()
    .toLowerCase()
    .indexOf(columnDataToFilter[propertyName].toString().toLowerCase()) >
    -1
);

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!