15

This is how I filter some data by title value:

data.filter(x => x.title.includes(term))

So data like

Sample one
Sample Two
Bla two

will be 'reduced' to

Bla two

if I'm filtering by two.

But I need to get the filtered result

Sample Two
Bla two
Cœur
  • 37,241
  • 25
  • 195
  • 267
user3142695
  • 15,844
  • 47
  • 176
  • 332

1 Answers1

50

You can use a case-insensitive regular expression:

// Note that this assumes that you are certain that `term` contains
// no characters that are treated as special characters by a RegExp.
data.filter(x => new RegExp(term, 'i').test(x.title));

A perhaps easier and safer approach is to convert the strings to lowercase and compare:

data.filter(x => x.title.toLowerCase().includes(term.toLowerCase()))
Trott
  • 66,479
  • 23
  • 173
  • 212