从阵列中提醒项目?

时间:2014-04-16 01:58:54

标签: javascript arrays

当页面打开时,我无法在警报中显示每个专辑的标题。我不确定在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()

2 个答案:

答案 0 :(得分:1)

  1. 首先,您遇到了一些语法错误 - 例如对象ID没有用引号括起来,而且数组中引用的元素不存在(没有提到whiteAlbum_Beatles个对象/ ID在你的代码?)

  2. 我认为您希望将对象存储在数组中,而不是对象ID

  3. 您可以使用以下内容:

    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 :(得分:0)

只需进行一些修正,您的代码就可以运行:

  1. ID 值应指定为字符串,因为它们尚未变量。
  2. Feed数组这些对象文字。即 fakeDatabase.push(foxyShazam,惊悚片,千禧年,darkSideOfTheMoon);
  3. 在这里:)

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

    欢呼声, 阿肖克

相关问题