Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何证明数组是否包含字符串?_Javascript_Jquery_Arrays_String - Fatal编程技术网

Javascript 如何证明数组是否包含字符串?

Javascript 如何证明数组是否包含字符串?,javascript,jquery,arrays,string,Javascript,Jquery,Arrays,String,以下代码所做的是获取所选类型类型,并检查它是否包含所有数组的数据之一 示例: genre = "comedy"; data = ["action", "comedy"] 我只想检查数据是否包含“喜剧”: 我尝试使用选择器“contains”来执行此操作,但我认为它不适用于此。您可以使用Array.indexOf()或使用$.inArray()如下: if( $.inArray(genre, data) > -1 ){ alert('contains') } 您可以这样做: $(

以下代码所做的是获取所选类型
类型
,并检查它是否包含所有数组的
数据之一

示例:

genre = "comedy";
data = ["action", "comedy"]
我只想检查
数据
是否包含
“喜剧”


我尝试使用选择器
“contains”
来执行此操作,但我认为它不适用于

您可以使用
Array.indexOf()
或使用
$.inArray()
如下:

if( $.inArray(genre, data) > -1 ){
    alert('contains')
}
您可以这样做:

$( "header section" ).selectable({
   selected: function() {

      var genre = $(".ui-selected").data("genre");
      var data = $("body > section div").map(function() {
                      return $(this).data("genre");
                  }).get(); // creates an array

      if($.inArray(genre, data) !== -1){//<--check if 'genre' exists in 'data'
          $(this).css("display", "block");
      }else{
          $(this).css("display", "none");
      }
   } 
});
$(“标题部分”)。可选({
所选:函数(){
变量类型=$(“.ui选定”).data(“类型”);
var data=$(“body>section div”).map(函数(){
返回$(此).data(“流派”);
}).get();//创建一个数组
如果($.inArray(流派,数据)!=-1){/
data.indexOf(流派)>-1
$( "header section" ).selectable({
   selected: function() {

      var genre = $(".ui-selected").data("genre");
      var data = $("body > section div").map(function() {
                      return $(this).data("genre");
                  }).get(); // creates an array

      if($.inArray(genre, data) !== -1){//<--check if 'genre' exists in 'data'
          $(this).css("display", "block");
      }else{
          $(this).css("display", "none");
      }
   } 
});