2014-04-16 30 views
0

我在打開頁面時無法在警報中顯示每個相冊的標題。我不確定我在for循環中訪問數組時遇到了什麼問題。任何線索?從數組中提醒項目?

我很抱歉,如果我是新手,這似乎是一個明顯的解決方案。我也必須使用JavaScript,請記住。我只是想知道我是否完全訪問for循環中的索引是錯誤的。

是for循環後的命令是不正確的?

var fakeDatabase = []; 

var foxyShazam = { 
    id: foxyShazam_FS, 
    title:'Foxy Shazam', 
    artist:'Foxy Shazam', 
    price: '$14.99', 
    releaseDate: new Date(1968, 10, 22), 
    Quantity: 50, 
    Trackinglist: ["Intro: Bombs Away", "Wanna-Be Angel", "Count Me Out", "Bye Bye Symphony", "Unstoppable", "Second Floor", "Oh Lord", "Connect", "The Only Way to My Heart...", "Killin' It", "Evil Thoughts"] 
}; 

var thriller = { 
    id: thriller_MJ, 
    title:'Thriller', 
    artist:'Michael Jackson', 
    price: '$12.99', 
    releaseDate: new Date(1982, 10, 30), 
    Quantity: 35, 
    Trackinglist: ["Wanna Be Startin Somethin", "Baby Be Mine", "The Girl Is Mine", "Thriller", "Beat It", "Billie Jean", "Human Nature", "P.Y.T. (Pretty Young Thing)", "The Lady in My Life"] 
}; 

var millennium = { 
    id: millennium_BSB, 
    title:'Millennium', 
    artist:'Backstreet Boys', 
    price: '$7.99', 
    releaseDate: new Date(1999, 4, 18), 
    Quantity: 15, 
    Trackinglist: ["Larger Than Life", "I Want It That Way", "Show Me the Meaning", "It's Gotta Be You", "I Need You Tonight", "Don't Want You Back", "Don't Wanna Lose You Now", "The One", "Back to Your Heart", "Spanish Eyes", "No One Else Comes Close", "The Perfect Fan"] 
}; 

var darkSideOfTheMoon = { 
    id: darkSideOfTheMoon_PinkFloyd, 
    title:'Dark Side of the Moon', 
    artist:'Pink Floyd', 
    price: '$14.99', 
    releaseDate: new Date(1973, 02, 01), 
    Quantity: 60, 
    Trackinglist: ["Speak to Me", "Breathe", "On the Run", "Time", "The Great Gig in the Sky", "Money", "Us and Them", "Any Colour You Like", "Brian Damage", "Eclipse"] 
}; 

fakeDatabase.push(whiteAlbum_Beatles, thriller_MJ, millennium_BSB, darkSideOfTheMoon_PinkFloyd); 

function displayAlbum() { 
    for (var i=0; i < fakeDatabase.length; i++) { 
    alert(title);} 
}; 

displayAlbum() 
+0

什麼是它目前在做什麼? – flauntster

回答

1
  1. 首先,你有一些語法錯誤 - 比如對象ID不是被包裹在報價和根本不存在的數組中引用的元素(有沒有提到whiteAlbum_Beatles對象/ ID在你的代碼?)

  2. 我想你想的對象自己存儲陣列中,而不是對象ID的

您可以使用下列內容:

fakeDatabase.push(foxyShazam, thriller, millennium, darkSideOfTheMoon); 

function displayAlbum() { 
    for (var i=0; i < fakeDatabase.length; i++) { 
     alert(fakeDatabase[i].title); 
    } 
}; 

jsFiddle here

P.S:以供將來參考,使用console.log()記得it's better to test

+1

爲什麼沒有人告訴我那個?我覺得我有錯誤的導師。感謝一羣jsFiddle和Ashok! – user3538317

+0

@ user3538317不用擔心,考慮選擇我的答案,如果它有幫助:) – lifetimes

0

剛夫婦校正和你的代碼的工作原理:因爲它們不是變量尚未

  1. ID值應該被指定爲字符串
  2. 給這些對象字面量數組。即fakeDatabase.push(foxyShazam,驚悚片,千禧年,darkSideOfTheMoon);

在這裏你去:)

<script type="text/javascript"> 
var fakeDatabase = []; 

var foxyShazam = { 
id: "foxyShazam_FS", 
title:'Foxy Shazam', 
artist:'Foxy Shazam', 
price: '$14.99', 
releaseDate: new Date(1968, 10, 22), 
Quantity: 50, 
Trackinglist: ["Intro: Bombs Away", "Wanna-Be Angel", "Count Me Out", "Bye Bye Symphony", "Unstoppable", "Second Floor", "Oh Lord", "Connect", "The Only Way to My Heart...", "Killin' It", "Evil Thoughts"] 
}; 

var thriller = { 
id: "thriller_MJ", 
title:'Thriller', 
artist:'Michael Jackson', 
price: '$12.99', 
releaseDate: new Date(1982, 10, 30), 
Quantity: 35, 
Trackinglist: ["Wanna Be Startin Somethin", "Baby Be Mine", "The Girl Is Mine", "Thriller", "Beat It", "Billie Jean", "Human Nature", "P.Y.T. (Pretty Young Thing)", "The Lady in My Life"] 
}; 

var millennium = { 
id: "millennium_BSB", 
title:'Millennium', 
artist:'Backstreet Boys', 
price: '$7.99', 
releaseDate: new Date(1999, 4, 18), 
Quantity: 15, 
Trackinglist: ["Larger Than Life", "I Want It That Way", "Show Me the Meaning", "It's Gotta Be You", "I Need You Tonight", "Don't Want You Back", "Don't Wanna Lose You Now", "The One", "Back to Your Heart", "Spanish Eyes", "No One Else Comes Close", "The Perfect Fan"] 
}; 

var darkSideOfTheMoon = { 
id: "darkSideOfTheMoon_PinkFloyd", 
title:'Dark Side of the Moon', 
artist:'Pink Floyd', 
price: '$14.99', 
releaseDate: new Date(1973, 02, 01), 
Quantity: 60, 
Trackinglist: ["Speak to Me", "Breathe", "On the Run", "Time", "The Great Gig in the Sky", "Money", "Us and Them", "Any Colour You Like", "Brian Damage", "Eclipse"] 
}; 

fakeDatabase.push(foxyShazam, thriller, millennium, darkSideOfTheMoon); 

function displayAlbum() { 
for (var i=0; i < fakeDatabase.length; i++) { 
alert(fakeDatabase[i].title);} 
}; 

displayAlbum() 
</script> 

歡呼聲, 阿肖克

+1

爲什麼發佈與我一樣的答案,即使使用相同的格式? – lifetimes