多属性数组语法

时间:2016-07-23 09:43:42

标签: javascript arrays

我无法为我想要创建的以下数组找到正确的语法。如果有人能指出我出错的地方,我将不胜感激。我想在点击按钮时生成随机电影;但是坚持这种语法。

由于

var thrillers =  [],
movieInfo[0] =   ("The Soccer Nanny", 5.7, 2011, "Drama", 94),
movieInfo[1] =   ( "Dawg Fight", 7.3, 2015 , "Sport", 79,
movieInfo[2] =   ( "Anatomy of a Love Seen", 5.2, 2014, "Romantic", 80),
movieInfo[3] =   ( "JFK – Director’s Cut", 8, 1991, "Thriller", 205),
movieInfo[4] =   ( "Common", 7.4, 2014, "Drama", 93),
movieInfo[5] =   ( "Altar", 6.1, 2014, "Horror", 88),
movieInfo[6] =   ( "Pokémon the Movie: Genesect and the Legend Awakened", 5.3, 2013, "Children", 71),
movieInfo[7] =   ( "Boy Meets Girl", 6.7, 2014, "Romantic", 99),
movieInfo[8] =   ( "Jack Irish: Dead Point", 6.6, 2014, "Thriller", 83),
movieInfo[9] =   ( "Jack Irish: Black Tide", 6.6, 2012, "Thriller", 93),
movieInfo[10] =  ( "Jack Irish: Bad Debts", 6.6, 2012, "Thriller", 99),
movieInfo[11] =  ( "He Who Dares", 2.9, 2014, "Action", 82),
movieInfo[12] =  ( "The King Is Dead!", 5.5, 2012, "Comedy", 106),
movieInfo[13] =  ( "Julia X", 4.5, 2011, "Horror", 92);

1 个答案:

答案 0 :(得分:0)

如果你想要一个数组数组,你可以使用与外部数组相同的语法:

var thrillers = [];
thrillers[0] = ["The Soccer Nanny", 5.7, 2011, "Drama", 94];
thrillers[1] = ["Dawg Fight", 7.3, 2015 , "Sport", 79];
// ...

但你可以一起做到这一切:

var thrillers = [
    ["The Soccer Nanny", 5.7, 2011, "Drama", 94],
    ["Dawg Fight", 7.3, 2015 , "Sport", 79],
    // ...
];

但是我不会做一个阵列数组;我做了一系列对象,每个对象的部分都有有用的名称:

var thrillers = [
    {title: "The Soccer Nanny", something: 5.7, year: 2011, category: "Drama", rating: 94},
    {"Dawg Fight", something: 7.3, year: 2015 , category: "Sport", rating: 79},
    // ...
];

然后你使用这样的:

console.log(thrillers[0].title); // "The Soccer Nanny"

和/或

var thriller = thrillers[0];
console.log(thriller.title); // "The Soccer Nanny"