2017-07-28 41 views
0

我有一个数组,我想存储名称,然后是一条信息,然后我可以调用该条目来检索信息。当我尝试搜索数组首先检查的项目是数组中我收到:GenreArray.indexOf is not a function无法抓取indexOf - 不是函数

我的代码:

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

GenreArray是对象,对象包含键/缬氨酸对,没有索引。你可以做hasOwnProperty

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

} 

(所以它可能是有意义的重命名该变量为“GenreObj”)

+0

如果我是把它变成带[]数组;我怎样才能做'00':“包含Amy Whinehouse',因为我在元素列表错误后收到了'missing' – irishwill200

+0

@ irishwill200 - 不确定你的意思。你想要数组看起来/ – tymeJV

+0

我想存储信息密钥:信息,所以我可以检查密钥是否存在,如果是这样的话..使用'array [key] [0]或者array [key]将信息显示为'info'' – irishwill200