嵌套数组问题

时间:2013-04-19 19:59:58

标签: javascript arrays

我没有在javascript中尝试嵌套数组,所以我不确定它们的格式。这是我的数组:

var items = [
    {"Blizzaria Warlock", "105341547"},
    {"Profit Vision Goggles", "101008467"},
    {"Classy Classic", "111903124"},
    {"Gold Beach Time Hat", "111903483"},
    {"Ocher Helm of the Lord of the Fire Dragon", "111902100"},
    {"Greyson the Spiny Forked", "102619387"},
    {"Egg on your Face", "110207471"},
    {"Evil Skeptic", "110336757"},
    {"Red Futurion Foot Soldier", "90249069"},
    {"Wizards of the Astral Isles: Frog Transformer", "106701619"},
    {"Dragon's Blaze Sword", "105351545"}
];
alert(items[2][1]);

...应提醒111903124但不提醒。

2 个答案:

答案 0 :(得分:6)

使用

var items = [
    ["Blizzaria Warlock", "105341547"],
    ["Profit Vision Goggles", "101008467"],
    ["Classy Classic", "111903124"],
    ...
    ["Dragon's Blaze Sword", "105351545"]
];

将数组构建到数组中。没有理由改变语法因为它们在里面。

答案 1 :(得分:0)

对象({})是键值对的集合。您的对象{"Blizzaria Warlock", "105341547"}包含值但没有键。也许更好的方法是为这些属性提供描述性名称,然后按属性名称引用项目:

var items = [
    {name: "Blizzaria Warlock", val: "105341547"},
    {name: "Profit Vision Goggles", val: "101008467"},
    {name: "Classy Classic", val: "111903124"},
    {name: "Gold Beach Time Hat", val: "111903483"},
    {name: "Ocher Helm of the Lord of the Fire Dragon", val: "111902100"},
    {name: "Greyson the Spiny Forked", val: "102619387"},
    {name: "Egg on your Face", val: "110207471"},
    {name: "Evil Skeptic", val: "110336757"},
    {name: "Red Futurion Foot Soldier", val: "90249069"},
    {name: "Wizards of the Astral Isles: Frog Transformer", val: "106701619"},
    {name: "Dragon's Blaze Sword", val: "105351545"}
];
alert(items[2].val);

val可以替换为更具描述性的内容,例如points