1

A site I am working on will require a list of makes (car manufacturers) listed in one column, the models of that specific make appear in the column to the right of it when a make is clicked, and will then proceed to list the details for available cars of that model in a div on the right-hand side. The majority of that code is completed, however I've needed to change my array from one filled with object literals to having an array with object literals within an array. Below is the code that I have been trying to work out (the arrays only, I can handle the rest of it) and I just can't figure out what to do to get it right.

Any help would be greatly appreciated.

<div id="test"></div>

<script type="text/javascript">

var inv=new Array();
    inv["ASTON MARTIN"]=new Array(
        ["DBS"]=new Array(
            {"year":2009;"price":"$191,400"},
            {"year":2006,"price":"$160,000"});
        ["DB5"]=new Array(
            {"year":2000,"price":"$80,500"},
            {"year":1996,"price":"$100,600"});              
    );

document.getElementById('test').innerHTML = inv["ASTON MARTIN"]["DBS"][1].price;

</script>

2 Answers 2

3

In your code, inv should not be an array, but an object.

var inv = {};

You can not use strings as array index, but using an object looks fine in your case.

same thing for second level.

var inv = {};
    inv["ASTON MARTIN"] = {
        "DBS": new Array(
            {"year":2009;"price":"$191,400"},
            {"year":2006,"price":"$160,000"}),
        "DB5": new Array(
            {"year":2000,"price":"$80,500"},
            {"year":1996,"price":"$100,600"})             
    };

Or even simpler:

var inv = {};
inv["ASTON MARTIN"] = {
    "DBS": [
        {"year":2009;"price":"$191,400"},
        {"year":2006,"price":"$160,000"}
    ],
    "DB5": [
        {"year":2000,"price":"$80,500"},
        {"year":1996,"price":"$100,600"
    ]
};
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much. So when referencing it, something such as document.getElementById('test').innerHTML = inv["ASTON MARTIN"]["DBS"][1].price; would return a value of $160,000, or do I need to leave out the brackets encasing "DBS"?
your comment was truncated, but what you used inv["ASTON MARTIN"]["DBS"][1].price should work.
Is there a way to have it go through a for loop and pull all the makes into a variable? I've tried var makes = '' for (var i=0; i<inv["ASTON MARTIN"].length;i++){ makes += inv["ASTON MARTIN"][i]; } However having the makes as anything but their specific name seems to give an error stating it's undefined.
Ugh, I'm sorry, it's late. I meant to say models. Is it possible to pull all models (DBS, DB5) through a for loop using numerical indexes, or are they only going to be able to be referenced by names?
you'll need 2 levels of loops, one to iterate over the makes, one to iterate over the models using the same for (... in ...) construct.
0

Inv should be object as you want indexing by names, not numerical indexes:

inv = {
    "ASTON MARTIN": {
        "DBS": [
            {"year":2009;"price":"$191,400"},
            {"year":2006,"price":"$160,000"}
               ],
        "DB5": [
            {"year":2000,"price":"$80,500"},
            {"year":1996,"price":"$100,600"});              
               ]
      },
   "CAR":{
          "Type": []
       }
   };

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.