代码之家  ›  专栏  ›  技术社区  ›  William

无法抓取indexOf-不是函数

  •  0
  • William  · 技术社区  · 8 年前

    我有一个数组,我想存储名称和一段信息,这样我就可以调用该项来检索信息。当我尝试搜索数组以首先检查时,项目位于我收到的数组中: GenreArray.indexOf is not a function

    var GenreArray = {"00's":"Includes Amy Whinehouse, Westlife, The Killers...","90's":"Includes Britney Spears, Backstreet Boys, Mariah Carey..."};
    
    if(GenreArray.indexOf("00's") > -1) //Crashed here
    {
     //Code here
      console.log(GenreArray["00's"]);
    }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   tymeJV    8 年前

    GenreArray 是一个对象,对象包含键/值对,而不是索引。你能做到的 hasOwnProperty

    if (GenreArray.hasOwnProperty("00's")) {
    
    }
    

    (因此将该变量重命名为“GenreObj”可能有意义)