不能抓取索引 - 不是一个函数



我有一个数组,我想存储名称和一条信息,这样我就可以调用该项目来检索信息。当我尝试搜索数组以首先检查项目是否在我收到的数组中时: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"]);
}

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

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

(因此,将该变量重命名为"GenreObj"可能是有意义的(

最新更新