{"id":344,"date":"2012-03-05T00:53:19","date_gmt":"2012-03-05T00:53:19","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=344"},"modified":"2012-03-05T00:53:19","modified_gmt":"2012-03-05T00:53:19","slug":"javascript-check-if-a-value-is-in-an-array","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/javascript-check-if-a-value-is-in-an-array\/","title":{"rendered":"Javascript Check If A Value Is In An Array Examples"},"content":{"rendered":"<p>It&#8217;s relatively very easy in Javascript to check if a value is in an array. All you need is the array that you want to check and the value that you want to check against. That&#8217;s it. Using just those 2 pieces of information, you can <em><strong>easily check if a value is in an array or not using JavaScript<\/strong><\/em>. Read on to find out more.<!--more--><\/p>\n<h2>How to use Javascript to Check if a value is in an array &#8211; Examples<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=344\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<h3>Example 1: Here is the full JavaScript code you need to check if a value is in an array or not:<\/h3>\n<pre class=\"lang:js decode:true\">my_array = Array();\n\n\/\/To find if a value or element exists in an array\nif (my_array.indexOf(\u2018find_this_value\u2019) != -1)\n{\nalert(\u2018Value exists in array.\u2019);\n}\n\n\/\/To find if a value or element DOES NOT exist in an array\nif (my_array.indexOf(\u2018find_this_value\u2019) == -1)\n{\nalert(\u2018Value does not exist in array.\u2019);\n}<\/pre>\n<h3>Example 2: Check whether a string is a part of an array value i.e. match a string with just part of the array value<\/h3>\n<pre class=\"lang:js decode:true\" title=\"Check whether a string is a part of an array value i.e. match a string with just part of the array value\">function search_for_string_in_array(search_for_string, array_to_search) \n{\n    for (var i=0; i&lt;array_to_search.length; i++) \n\t{\n        if (array_to_search[i].match(search_for_string))\n\t\t{ \n\t\t\treturn 'Value exists in array';\n\t\t}\n    }\n\n    return 'Value does NOT exist in array';\n}\n\nvar my_array = [\"test one\", \"test two\", \"test three\"];\n\nalert( search_for_string_in_array ('two', my_array) );<\/pre>\n<p>Simply pass the value to search for and the array to the function and it will tell you whether the string exists as a part of an array value or not.<\/p>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=344\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<h4>Simple, isn&#8217;t it?<\/h4>\n<p>Do you know of any other way to <em><strong>check\u00a0\u00a0if a value or an element exists in an array or not in JavaScript<\/strong><\/em>? If yes, please share your tips with us by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s relatively very easy in Javascript to check if a value is in an array. All you need is the array that you want to check and the value that you want to check against. That&#8217;s it. Using just those 2 pieces of information, you can easily check if a value is in an array [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":352,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[17,13],"tags":[35],"class_list":["post-344","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript-tips-and-tricks","category-tips-and-tricks","tag-javascript-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-5y","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=344"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/344\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}