-4

我对这段代码做错了什么,我无法让它打印出任何团队的名称。我希望它输出第一队或第二队等的名称。但没有任何效果。有什么帮助吗?

   var seeds = [
        { "id" : 1, "name" : "Team 1" }
        { "id" : 2, "name" : "Team 2" }
        { "id" : 3, "name" : "Team 3" }
        { "id" : 4, "name" : "Team 4" }
        { "id" : 5, "name" : "Team 5" }
        { "id" : 6, "name" : "Team 6" }
        { "id" : 7, "name" : "Team 7" }
        { "id" : 8, "name" : "Team 8" }
    ]
    document.write(seeds[0].name);
4

4 回答 4

7

你忘记了逗号:

var seeds = [
    { "id" : 1, "name" : "Team 1" },
    { "id" : 2, "name" : "Team 2" },
    { "id" : 3, "name" : "Team 3" },
    { "id" : 4, "name" : "Team 4" },
    { "id" : 5, "name" : "Team 5" },
    { "id" : 6, "name" : "Team 6" },
    { "id" : 7, "name" : "Team 7" },
    { "id" : 8, "name" : "Team 8" }
];

seeds是一个Array,你必须用逗号分隔元素,如下所示:

var s = [{},{},{},{}];

请注意,最后一个元素后没有逗号。

于 2012-12-21T15:18:06.350 回答
2

每个对象之间应该有一个逗号

var seeds = [
    { "id" : 1, "name" : "Team 1" },
    { "id" : 2, "name" : "Team 2" },
    { "id" : 3, "name" : "Team 3" },
    { "id" : 4, "name" : "Team 4" },
    { "id" : 5, "name" : "Team 5" },
    { "id" : 6, "name" : "Team 6" },
    { "id" : 7, "name" : "Team 7" },
    { "id" : 8, "name" : "Team 8" }
]
document.write(seeds[0].name);
于 2012-12-21T15:18:15.123 回答
2

您缺少,数组中的

var seeds = [
    { "id" : 1, "name" : "Team 1" },
    { "id" : 2, "name" : "Team 2" },
    { "id" : 3, "name" : "Team 3" },
    { "id" : 4, "name" : "Team 4" },
    { "id" : 5, "name" : "Team 5" },
    { "id" : 6, "name" : "Team 6" },
    { "id" : 7, "name" : "Team 7" },
    { "id" : 8, "name" : "Team 8" }
]
document.write(seeds[0].name);
于 2012-12-21T15:18:15.317 回答
2

在元素之间放置逗号:

var seeds = [
    { "id" : 1, "name" : "Team 1" },
    { "id" : 2, "name" : "Team 2" },
    { "id" : 3, "name" : "Team 3" },
    { "id" : 4, "name" : "Team 4" },
    { "id" : 5, "name" : "Team 5" },
    { "id" : 6, "name" : "Team 6" },
    { "id" : 7, "name" : "Team 7" },
    { "id" : 8, "name" : "Team 8" }
]
document.write(seeds[0].name);
于 2012-12-21T15:18:45.053 回答