3

On the given array:

const arr = [
  { fn: "Steve", ln: "Jobs" },
  { fn: "Dennis", ln: "Rodman" },
  { fn: "Karl", ln: "Malone" },
  { fn: "Vince", ln: "Carter" },
  { fn: "Steve", ln: "King" }
];

If I want to return a new array excluding anyone called Steve, I could run:

const filteredArr = arr.filter(obj => obj.fn != "Steve");

Without using toLowerCase(), How could I write a regex to achieve the same result in the case where Steve names could be lower or upper case?

  { fn: "Steve", ln: "Jobs" },
  { fn: "Dennis", ln: "Rodman" },
  { fn: "Karl", ln: "Malone" },
  { fn: "Vince", ln: "Carter" },
  { fn: "steVe", ln: "King" },
  { fn: "STEVE", ln: "Jordan" },
  { fn: "steve", ln: "Clark" },
Null isTrue
  • 1,882
  • 6
  • 26
  • 46
  • If you know that you want to use regex, have you tried looking up something like "case-insensitive string matching with regex"? I would imagine with that information, you could easily incorporate that into your original example... – Tyler Roper Jan 17 '19 at 20:41
  • Possible duplicate of [ES6: Filter data with case insensetive term](https://stackoverflow.com/questions/44469548/es6-filter-data-with-case-insensetive-term) – adiga Jan 17 '19 at 20:46
  • @adiga the referenced question was poorly asked imo. `"data like Bla two?"`. I specifically asked and provided a real code example with a structure of what my problem was. – Null isTrue Jan 17 '19 at 20:55
  • @NullisTrue the accepted answer is exactly same as the one in this question. – adiga Jan 18 '19 at 07:22

3 Answers3

5

Check the strings with RegExp.test() method, and use the i flag (ignore case) in the regular expression.

Note: if you want exact matches, ie ignore strings that include the name - "steven" for example - you can use ^steve$ (see boundaries).

const arr = [{"fst":"Steve","snd":"Jobs"},{"fst":"Dennis","snd":"Rodman"},{"fst":"Karl","snd":"Malone"},{"fst":"Vince","snd":"Carter"},{"fst":"steVe","snd":"King"},{"fst":"STEVE","snd":"Jordan"},{"fst":"steve","snd":"Clark"}];

const pattern = /steve/i; // or /^steven$/i
const result = arr.filter(obj => !pattern.test(obj.fst));

console.log(result);

And you can use the RegExp constructor to make it reusable:

const arr = [{"fst":"Steve","snd":"Jobs"},{"fst":"Dennis","snd":"Rodman"},{"fst":"Karl","snd":"Malone"},{"fst":"Vince","snd":"Carter"},{"fst":"steVe","snd":"King"},{"fst":"STEVE","snd":"Jordan"},{"fst":"steve","snd":"Clark"}];

const removeItem = (arr, str, key = 'fst') => {
  const pattern = new RegExp(str, 'i'); // or new RegExp(`^${str}$`, 'i')

  return arr.filter(obj => !pattern.test(obj[key]));
};

const result = removeItem(arr, 'steve');

console.log(result);
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
  • Thank you @Ori Drori, for some reason I thought this would've involved `new RegExp( ... )` Ah just saw your edited answer... – Null isTrue Jan 17 '19 at 20:48
2

const arr = [
  { fn: "Steve", ln: "Jobs" },
  { fn: "Dennis", ln: "Rodman" },
  { fn: "Karl", ln: "Malone" },
  { fn: "Vince", ln: "Carter" },
  { fn: "Steve", ln: "King" }
];

const filteredArr = arr.filter(obj => !/Steve/i.test(obj.fn));

console.log(filteredArr);

The i regex flag allows you to match results regardless of capitalization (uppercase/lowercase).

Itai Steinherz
  • 777
  • 1
  • 9
  • 19
2

Why not use .localeCompare(compareString[, locales[, options]])?

The localeCompare() method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.

In the options you may set the sensitivity:

"accent": Only strings that differ in base letters or accents and other diacritic marks compare as unequal. Examples: a ≠ b, a ≠ á, a = A.

const arr = [
  { fn: "Steve", ln: "Jobs" },
  { fn: "Dennis", ln: "Rodman" },
  { fn: "Karl", ln: "Malone" },
  { fn: "Vince", ln: "Carter" },
  { fn: "Steve", ln: "King" }
];

const filteredArr = arr.filter(obj => obj.fn.localeCompare("steve", undefined, 
                                                        { sensitivity: 'accent' }));
console.log(filteredArr);
gaetanoM
  • 41,594
  • 6
  • 42
  • 61
  • 2
    Nice! The 0 answer (equal) evaluates to `false` and the item is removed. You should include more info about this solution. – Ori Drori Jan 17 '19 at 20:55