Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.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 - Fatal编程技术网

Javascript 在对象数组中搜索字符串

Javascript 在对象数组中搜索字符串,javascript,jquery,Javascript,Jquery,我试图搜索对象数组中包含的字符串 image_array = [ {image: '/assets/monkey-face.jpg', link_url: '/animal/showit?animal=Monkey'}, {image: '/assets/tiger-face.jpg', link_url: '/animal/showit?animal=Tiger'} ] 在url参数showit或图像中找不到字符串“monkey”或“tig

我试图搜索对象数组中包含的字符串

image_array = [
            {image: '/assets/monkey-face.jpg', link_url: '/animal/showit?animal=Monkey'},
            {image: '/assets/tiger-face.jpg', link_url: '/animal/showit?animal=Tiger'} 

]
在url参数showit或图像中找不到字符串“monkey”或“tiger”

    image_array.IndexOf 
jQuery.inArray
如果我尝试循环遍历数组/对象并搜索得到的字符串:

"Object #<Object> has no method 'search'"

我哪里出错了?当对象中有字符串时,为什么返回-1

数组没有搜索方法-它用于字符串。试一试

演示:

试试这个:

for(var i = 0; i < image_array.length; i++){
  if(image_array[i].link_url.indexof("Monkey") != -1){
    // Item found
  }
}

由于您试图检查对象值中是否包含字符串,因此假设该字符串未用作任何键值,请尝试以下操作:

 return JSON.stringify(image_array).indexOf("monkey") > 0

希望这有帮助:

您是否尝试过图像的src属性?嗯,错误似乎很明显。您的数组包含对象而不是字符串,并且对象没有方法searchArun。我需要数组中对象的索引,这样接受的答案对我来说最合适。
 return JSON.stringify(image_array).indexOf("monkey") > 0