jquery - how to use inArray() to find string in array element if string is surrounded by other characters -
How do I find an array using the value inArray ()
and return True
even if that value is surrounded by other characters. Here I have the code ( med
is my array, it is med [1-10]
):
AlpariZolmog = $ INRRE ('Alpajolam', Made) & gt; -1; Xanaxlog = $ .inArray ('XX', Made) & gt; -1; If (alpazolmog == true) {$ ("# xanax"). CSS ("display", "block"); } And if (xanaxlog == true) {$ ("# xanax"). CSS ("display", "block"); }
returns true
if the array element is "xanax" or "alprazolam" but if a user accesses "xanax xr" or "alprazolam er" Will return the false
if there is any way to search for the string "xanax" or "alprazolam" at any point in that particular array element, even if that particular array element contains other characters, And in this case to return true
?
InArray () can not be used, it is looking for partial matches again, it does not work just like that instead, you can only restart yourself and you want that array Match any type against each element.
$ There is no magic for InArray ()
. It's just a shortcut to get an exact value in an array, but if that shortcut is not exactly what you want, just compare your own repetition and your own. For example:
function findPartialStrInArray (array, target) {for (var i = 0; i & lt; array.length; i ++) {var item = array [i]; // If this array element is a string and contains the target string (if the item type === "string" & amp; amp; item.indexOf (target)! == -1) {return i; }} Return -1; } Var alprazolmog = SearchPortalAstronArere (Made, 'Alperazolam') & gt; -1; Var xanaxlog = findPartialStrInArray (med, 'xanax') & gt; -1;
Comments
Post a Comment