Alex
Alex

Reputation: 1055

Nested Arrays Issue

I haven't tried nested arrays in javascript, so im not sure of the formtting for them. Here's my array:

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]);

...which should alert 111903124, but doesn't.

Upvotes: 0

Views: 69

Answers (2)

Denys Séguret
Denys Séguret

Reputation: 382092

Use

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

to build arrays into arrays. No reason to change syntax because they're inside.

Upvotes: 6

Rick Viscomi
Rick Viscomi

Reputation: 8852

Objects ({}) are collections of key-value pairs. Your object {"Blizzaria Warlock", "105341547"} contains values but no keys. Perhaps a better approach would be to give these properties descriptive names, then reference the items by property name:

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 can be replaced with something more descriptive, like points.

Upvotes: 0

Related Questions