0

I have this if else expression filtering elements inside dogs[] courses[] departments[] the first snippet definitely works the way I want but my problem is its not reusable and the way is es5-ish.

1st snippet

    if (item == 'dogs') {
      item = 'dogs';
      return this[`${item}`].filter(item => new RegExp(`^${data}`, 'gi').test(item));
    } else if (item == 'courses') {
      item = 'courses';
      return this[`${item}`].filter(item => new RegExp(`^${data}`, 'gi').test(item));
    } else if (item == 'departments') {
      item = 'departments';
      this.dept = this.deptControl.value;
      return this[`${item}`].filter(item => new RegExp(`^${data}`, 'gi').test(item.name));
    }

In this snippet I tried my best to shorten the es5-ish code but the deptartments[] does not work, the way I want

2nd snippet

    item =
      item == 'dogs' ? 'dogs'
        : item == 'courses' ? 'courses'
        : item == 'departments' ? 'departments' : '';

    return this[`${item}`].filter(item => new RegExp(`^${data}`, 'gi').test(item == 'departments' ? item.name : item));

and the department[] in this snippet is likewise doesn't work the way I want

3rd snippet

    item =
     item == 'dogs' ? 'dogs'
        : item == 'courses' ? 'courses'
        : item == 'departments' ? 'departments' : '';

    // let patch = item == 'departments' ? item.name : item;
    let patch = item == 'departments' ? item['name'] : item;

    return this[`${item}`].filter(item => new RegExp(`^${data}`, 'gi').test(patch));

Please help me how to fix my issue in the departments[] block, using ternary operator, not the old style if..else

6
  • 1
    Parethesis won't bite. Commented Sep 16, 2017 at 14:01
  • 2
    Or just item = item || "" Commented Sep 16, 2017 at 14:02
  • 1
    You only set this.dept in the first block. Commented Sep 16, 2017 at 14:03
  • @adeneo where should i put the item = item || "" ? Commented Sep 16, 2017 at 14:05
  • You'd just replace the entire ternary with it? The second ternary, and the entire filter function, makes no sense. If item is a string, then get item.name, and you're filtering just one item ? Commented Sep 16, 2017 at 14:09

2 Answers 2

1

Just use a different variable name in the filter, like x:

return {dogs:1,courses:1,departments:1}[item] ? this[item].filter(x => 
        new RegExp(`^${data}`, 'gi').test(item == 'departments' ? x.name : x))
    : undefined;
Sign up to request clarification or add additional context in comments.

Comments

0

Given you're setting a value if it's department, I think perhaps a switch statement would be good here. How about something like this?

const testItem = (items, data) => {
  return items.filter(item => new RegExp(`^${data}`, 'gi').test(item))
}

switch (item) {
  case 'dogs':
  case 'courses':
    return testItem(this[item], data)
  case 'departments':
    this.dept = this.deptControl.value
    return testItem(this[item], data)
}

This is my suggestion for the following reasons:

  • Write code for humans, not computers. The more readable and maintainable solution is the better one.
  • Don't Repeat Yourself - try to put common operations into smaller functions (this is not only more maintainable, but the interpreter can make better optimisations)

Notice how the testItem function is reused, and the same instruction is executed when item is dogs or courses. Furthermore, although this sample doesn't have any equality operators, you should use === instead of == in Javascript :) (I know, it's confusing...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.