Raviteja
Raviteja

Reputation: 3489

How to remove strings from an array of strings which doesn't contain search keyword?

I've an array(testArray) of strings. I need to remove the strings from array which doesn't contain at least one of the searchTerms.testArray should ignore case of searchTerms.

EDIT: Result array should include the string even the search term is part of a word in the string.

eg:"someright text" should be included in the result.

var testArray = [
    "I am",
    "I am wrong and I don't know",
    "I am right and I know",
    "I don't know",
    "some text",
    "I do know"
  ],
  searchTerms = ["I", "right","know"] //or ["i", "right","know"];

$.each(searchTerms, function(index, term) {
  var regX = new RegExp(term, "i");
  testArray = $.map(testArray, function(item) {
    if (regX.test(item)) {
      return item;
    } else {
      return;
    }
  });
});

console.log(testArray);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

The result should be as below,

testArray = [
        "I am",
        "I am wrong and I don't know",
        "I am right and I know",
        "I don't know",           
        "I do know"
      ]

Upvotes: 0

Views: 67

Answers (2)

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28455

Use Array.filter, Array.some and Array.includes

var testArray = ["I am", "I am wrong and I don't know", "I am right and I know", "I don't know", "some text", "I do know"],
searchTerms = ["I", "right","know"];

searchTerms = searchTerms.map(w => w.toLowerCase());

/* Use filter to filter only those records which have the search term in it. */
var result = testArray.filter((s) => s.split(" ").some((w) => searchTerms.includes(w.toLowerCase())));

console.log(result);

EDIT

var testArray = ["I am", "I am wrong and I don't know", "I am right and I know", "I don't know", "some text", "I do know", "someright"],
searchTerms = ["I", "right","know"];

searchTerms = searchTerms.map(w => w.toLowerCase());

/* Use filter to filter only those records which have the search term in it. */
var result = testArray.filter((s) => searchTerms.some(w => s.toLowerCase().includes(w)));

console.log(result);

Upvotes: 3

HMR
HMR

Reputation: 39270

Here is what the code would look like adapting the code in the comment to your situation, the getter just returns a value from the array and not a property of the value.

testArray = [
  "I am",
  "I am wrong and I don't know",
  "I am right and I know",
  "I don't know",           
  "I do know"
];
const filterFn = getter => comparer => o =>
  comparer(getter(o));

//you are not getting a property of an object item in the array but the value
const getValue = o => o;

// compare search contains word
const containsWord = search => {
  //small optimization
  const searchAsRegExp = search.map(s=>new RegExp(s,"i"));
  return value =>
    searchAsRegExp.some(reg=>reg.test(value));
};

console.log(
  `using containsWord(["I", "right","know"])`,
  testArray.filter(filterFn(getValue)(containsWord(["I", "right","know"])))
)

Upvotes: 0

Related Questions