2017-07-28 27 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