-1

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?

Drake
  • 21
  • 4
  • An array's `filter` method enables any filter task. It is on the OP to precisely describe the requirements/condition according to which the filter task is going to be performed. Thus as for the mentioned three criteria/property values, does the OP, for each item, want to have every criteria (AND filter) being fulfilled (with the exception of the empty string) or at least one (OR filter)? – Peter Seliger May 18 '22 at 00:15

1 Answers1

1

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.
Carl HR
  • 776
  • 5
  • 12
  • This works if all values are not empty. If for example role is "" then it returns nothing. I still want to just filter by level and position if role is "". And vice versa with the others – Drake May 18 '22 at 00:59
  • I see.. your problem is that you must rephrase what you want to do, inside the if statement. We cannot guess your logic by divination. You must tell what kind of logic it should be. – Carl HR May 18 '22 at 19:36
  • What @Peter Seliger comented on your question is exactly this problem. You want us to guess what logic you should put inside the if statement, without telling us each rule you want to filter inside the array. For example, there's a difference between checking for `(role || level || position)` or `(role && level && position)` or `(role && level || position)` or `(role || level && position)`. – Carl HR May 18 '22 at 19:45
  • One thing that you can do, and I normally do it too, is to try to guess the correct logic. You don't need to write the right one in a single go. You can try to run the script many times using different logic combinations, and at some point you might guess the correct one to put inside the if statement. Just create an empty HTML file and test your script inside it. Any browser can run it. – Carl HR May 18 '22 at 19:48