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

124
Views
JavaScript how to filter based on multiple values if the values arent empty

I am trying to filter based on multiple values and only if they are not empty. Example:

jobsTracked = [
        {
          "company": "Company1",
          "position": "Senior Frontend Developer",
          "role": "Frontend",
          "level": "Senior",
        },
        {
          "company": "Company2",
          "position": "Senior Frontend Developer",
          "role": "Frontend",
          "level": "Senior",
          
        }]

I am trying to filter by 3 values in this example: position, role, level. And if position is empty string then i dont want to filter it but still want to filter by role and level if they arent empty string. And vice versa for all of them. Can the JavaScript filter function do this?

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

0

When you say "filtering", you mean something like this?

let slot;

let role = 'Frontend';
let level = 'Senior';
let position = 'Senior Frontend Developer';

let filtered = [];
let length = jobsTracked.length;
for (let i = 0; i < length; ++i) {
    slot = jobsTracked[i]; 
    
    // "The Filter"
    if ((slot['role'] != '' && slot['role'] == role) &&
        (slot['level'] != '' && slot['level'] == level) &&
        (slot['position'] != '' && slot['position'] == position)) {
        filtered.push(slot);
    }
}

What this script does:

  1. It iterates over all elements of the array jobsTracked.
  2. It filters only those elements which the properties are not empty strings, and match the declared variables of the same name.
  3. Only the filtered elements are added to the filtered array.
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!