lets say I have an array of filter strings that I need to loop and check against with other passed in string.
var filterstrings = [\'firststring\',\'secondst
convert filterstring and passedinstring to lowercase and compare
var filterstrings = ['firststring','secondstring','thridstring'];
var passedinstring =
localStorage.getItem("passedinstring").toLowerCase();
for (i = 0; i < filterstrings.lines.length; i++) {
if (passedinstring.includes(filterstrings[i].toLowerCase())) {
alert("string detected");
}
}
Fixed case sensitivity issue using toLowerCase()
. It turns all the string
to lower case while comparing.
var product=productList.filter((x) => x.Name.toLowerCase().includes(("Active").toLowerCase()))
ES6 array method filter() can simply the solution in single line, use includes() method to determines whether an array includes a certain value among its entries.
var filterstrings = ['firststring','secondstring','thridstring'];
var passedinstring = localStorage.getItem("passedinstring");
// convert each string from filterstrings and passedinstring to lowercase
// to avoid case sensitive issue.
filteredStrings = filterstrings.filter((str) => str.toLowerCase().includes(passedinstring.totoLowerCase())
You can simply convert the passedinstring to lowercase.
var passedinstring = localStorage.getItem("passedinstring").toLowerCase();
You can create a RegExp from filterstrings
first
var filterstrings = ['firststring','secondstring','thridstring'];
var regex = new RegExp( filterstrings.join( "|" ), "i");
then test
if the passedinstring
is there
var isAvailable = regex.test( passedinstring );
My option is comparing UPPER with UPPER or lower with lower transforming both sides (i did it often in SQL):
var filterstrings = ['firststring','secondstring','thirDstrIng'];
var passedinstring = 'ThIrDsTrInG3';
//used for of just to make it more readable
for (filterstring of filterstrings) {
if (passedinstring.toUpperCase().includes(filterstring.toUpperCase())) {
alert("string detected");
}
}
Prompts string detected