0

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.

2
  • You're currently trying to put ill-formed objects into an array. Commented Apr 19, 2013 at 20:02
  • That's an array of illegal syntax units. Use [[...], [...], ...] for an array of arrays or [{prop:value, ...}, ...] for an array of objects. Commented Apr 19, 2013 at 20:02

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

A ] is missing, and there is an excedent comma (I assume that you just want to put the first lines but it's not very clear).
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.