搜索对象数组的字符串

 卟懵de珍惜_463 发布于 2023-02-13 12:10

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

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 # has no method 'search'"


我哪里错了 - 为什么当对象中包含字符串时它返回-1?

1 个回答
  • 试试这个:

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

    2023-02-13 12:12 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有