{"id":5039,"date":"2023-04-22T14:15:00","date_gmt":"2023-04-22T19:15:00","guid":{"rendered":"https:\/\/www.incredigeek.com\/home\/?p=5039"},"modified":"2023-05-13T00:59:01","modified_gmt":"2023-05-13T05:59:01","slug":"check-if-a-string-matches-any-element-in-an-array","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/check-if-a-string-matches-any-element-in-an-array\/","title":{"rendered":"JavaScript check if a string matches any element in an Array"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/04\/image-11.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"366\" src=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/04\/image-11-1024x366.png\" alt=\"\" class=\"wp-image-5044\" srcset=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/04\/image-11-1024x366.png 1024w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/04\/image-11-300x107.png 300w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/04\/image-11-768x274.png 768w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/04\/image-11-500x179.png 500w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2023\/04\/image-11.png 1033w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>In the following code we will be checking a string and check if any of the words in the string match some or any elements in an array.<\/p>\n\n\n\n<p>We can imagine that our &#8220;stringToCheck&#8221; variable is an email or message response.  We want to know if it contains a mention to afternoon, tomorrow, or evening.  Presumably so we can automate something.  Note that the matches are case sensitive.<\/p>\n\n\n\n<pre id=\"CheckifStringMatchesJS\" class=\"wp-block-code has-black-color has-text-color\"><code lang=\"javascript\" class=\"language-javascript line-numbers\">\/\/ Check if any text in a string matches an element in an array\n\nconst stringToCheck = \"Let's grab lunch tomorrow\";\nconst arrayToCompareTo =[\"afternoon\", \"tomorrow\", \"evening\"];\n\n\/\/ We are checking to see if our string \"stringToCheck\" \n\/\/ Has any of the words in \"arrayToCompareTo\".\n\/\/ If it does, then the result is true.  Otherwise false.\nconst resultsOfCompare = arrayToCompareTo.some(checkVariable =&gt; stringToCheck.includes(checkVariable));\n\nif (resultsOfCompare == true){\n    console.log(stringToCheck + \" Contains a value in our Array \" + arrayToCompareTo);\n} else {\n    console.log(stringToCheck + \" Does NOT contain a value in our Array \" + arrayToCompareTo);\n}<\/code><\/pre>\n\n\n\n<p>More examples and ways to do it are available at the following link.<\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/37428338\/check-if-a-string-contains-any-element-of-an-array-in-javascript\">https:\/\/stackoverflow.com\/questions\/37428338\/check-if-a-string-contains-any-element-of-an-array-in-javascript<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the following code we will be checking a string and check if any of the words in the string match some or any elements in an array. We can imagine that our &#8220;stringToCheck&#8221; variable is an email or message &hellip; <a href=\"https:\/\/www.incredigeek.com\/home\/check-if-a-string-matches-any-element-in-an-array\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1460,792,1],"tags":[740,1197,1437,1474,1473],"class_list":["post-5039","post","type-post","status-publish","format-standard","hentry","category-javascript","category-programming","category-uncategorized","tag-array","tag-element","tag-javascript","tag-match","tag-string"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5039","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=5039"}],"version-history":[{"count":8,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5039\/revisions"}],"predecessor-version":[{"id":5131,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5039\/revisions\/5131"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=5039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=5039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=5039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}