简体   繁体   English

警报数组中的项目?

[英]Alerting items from an array?

I'm having trouble displaying the title of each album in an alert when the page opens. 页面打开时,我无法在警报中显示每个专辑的标题。 I am not sure what I am doing wrong with the access of the array in the for loop. 我不确定在for循环中访问数组有什么问题。 Any clues? 有什么线索吗?

I apologize if I am new to this and this seems like an obvious solution. 如果这是我的新手,我深表歉意,这似乎是一个显而易见的解决方案。 I have to use JavaScript too, please keep in mind. 我也必须使用JavaScript,请记住。 I am just wondering if I am completely accessing the indexes in the for loop the wrong way. 我只是想知道我是否以错误的方式完全访问了for循环中的索引。

Is it the command after the for loop that is incorrect? 是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()
  1. First, you had some syntax errors - such as object ID's not being wrapped in quotes and an element referenced in the array that didn't exist (there wasn't a whiteAlbum_Beatles object/ID mentioned in your code?) 首先,您遇到了一些语法错误-例如,对象ID没有用引号引起来,并且数组中引用的元素不存在(代码中没有提到whiteAlbum_Beatles对象/ ID?)

  2. I think you wanted to store the objects themselves in your array, not the object ID's . 我认为您想将对象本身存储在数组中,而不是对象ID的

You could use the following: 您可以使用以下内容:

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

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

jsFiddle here jsFiddle在这里

PS: for future reference, remember it's better to test using console.log() . PS:为了将来参考,请记住最好使用console.log() 进行测试

Just couple of correction and your code works: 只需进行几处更正,您的代码就会起作用:

  1. ID values should be assigned as string since they are not variable yet. 由于ID值尚未更改 ,因此应将其分配为字符串
  2. Feed array these object literals. 提要数组这些对象文字。 ie fakeDatabase.push(foxyShazam, thriller, millennium, darkSideOfTheMoon); fakeDatabase.push(foxyShazam,惊悚片,千年,darkSideOfTheMoon);

Here you go :) 干得好 :)

<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>

cheers, Ashok 干杯,阿肖克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM