简体   繁体   中英

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"
      ]

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); 

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"]))) ) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM