1

I have an input where you can search for a specific course by its name, for this I use filtering, and also the includes() method, my problem is that I need to apply three methods at the same time, the first method is toLowerCase(), the second is toUpperCase() and the last method is trim()

GetMyCourses() {
  return this.courses.filter(value => {
    return value.title.toLowerCase().includes(this.result);
  });
},

If you leave the toLowerCase method, then it does not give capital names when searching, and vice versa, in addition, if you remove these two methods, then when searching you will need to accurately search for the word

2
  • You could do this with array some, but here you certainly don't need it. It's like the answer says Commented Oct 16, 2021 at 21:03
  • How to use multiple methods in JavaScript at the same time use regex, .filter(value => new RegExp(search, "i").test(value.title)), search being something you made safe or built from some logic playcode.io/823069 Commented Oct 16, 2021 at 21:12

1 Answer 1

4

Convert this.result to lower case as well.

GetMyCourses() {
  let result = this.result.toLowerCase()
  return this.courses.filter(value => {
    return value.title.toLowerCase().includes(result);
  });
},
Sign up to request clarification or add additional context in comments.

4 Comments

Or just value.title.toLowerCase().includes(this.result.toLowerCase())...
@HereticMonkey Yeah, but that converts this.result multiple times unnecessarily.
Meh. 99% of the time that's not going to be a problem. If you want to micro-optimize, you should have a value.searchableTitle property that is already lower-cased...
I also like to do it just to keep the expression less complex.

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.