-2

I have some data on cars and am trying to create a function where I can get info on a specific car. I can't seem to format my data list correctly. I run it through JSLint and it tells me it is wrong but I don't know how to fix it.

var models = {
  "2009": 
    {Jaguar: [model: "XF", id: "123"], [model: "XK", id: "456"], [model: "XJ", id: "789"]}, 
    {Volvo: [model: "XC70", id: "223"], [model: "V50", id: "256"]}, 
  "2010": 
    {Jaguar: [model: "XF", id: "323"], [model: "XK", id: "356"]}, 
    {Volvo: [model: "XC70", id: "423"], [model: "V50", id: "456"]}
  };
   //**** Show an example model here
    console.log(models.2010.Jaguar[0].model);
Thread7
  • 1,081
  • 2
  • 14
  • 28

3 Answers3

3

You could change to something like this:

var models = {
    "2009":
            {
                Jaguar: [{model: "XF", id: "123"}, {model: "XK", id: "456"}, {model: "XJ", id: "789"}],
                Volvo: [{model: "XC70", id: "223"}, {model: "V50", id: "256"}]
            },
    "2010":
            {
                Jaguar: [{model: "XF", id: "323"}, {model: "XK", id: "356"}],
                Volvo: [{model: "XC70", id: "423"}, {model: "V50", id: "456"}]
            }
};
//**** Show an example model here
console.log(models['2010'].Jaguar[0].model);
Vinícius Fagundes
  • 1,983
  • 14
  • 24
  • `"2009": {Jaguar: ...}, {Volvo: ...}` and `models.2010` are syntax errors. (I will upvote when you correct it.) – Amadan Jan 05 '16 at 00:35
  • Yes. One moment. It's hard to make a code with this editor. Need more `{`,`}` – Vinícius Fagundes Jan 05 '16 at 00:38
  • A good way to make sure your code is correct: put it into a REPL. Fortunately, JS runs in a browser, and a JS console is only three keys away. Or you can use jsbin, or jsfiddle, or stackoverflow snippets, or node interactive shell, and copy-paste the correct solution from there. There are sometimes problems where it is not feasible to test the solution; this is not one of them :D – Amadan Jan 05 '16 at 00:41
3
  • models is correctly an object (indexed by "2009" and "2010").
  • models["2009"] should be a single object; you can't have two values. {Jaguar:...}, {Volvo:...} is wrong, needs to be {Jaguar: ..., Volvo:}.
  • Drilling further down, again, Jaguar: ..., ..., ... is wrong - only one value per index. In this case, you want this value to be an array: Jaguar: [..., ..., ...].
  • Array literals can't have named attributes, so [model: "XF", id: "123"] is incorrect; this should be an object, {model: "XF", id: "123"}.
  • You can use the dot notation only with identifiers; 2010 is not a valid identifier, so models.2010 is an error. With attributes whose names are not identifiers, you must use the bracket notation: models["2010"].
Amadan
  • 191,408
  • 23
  • 240
  • 301
2
var models = {
  "2009": {
    Jaguar: [{model: "XF", id: "123"}, {model: "XK", id: "456"}, {model: "XJ", id: "789"}], 
    Volvo: [{model: "XC70", id: "223"}, {model: "V50", id: "256"}]
  }, 
  "2010": {
    Jaguar: [{model: "XF", id: "323"}, {model: "XK", id: "356"}], 
    Volvo: [{model: "XC70", id: "423"}, {model: "V50", id: "456"}]
  }
};

console.log(models[2010].Jaguar[0].model);

NOTE: models[2010] because it can't be accessed with dot notation.

Eduardo Escobar
  • 3,301
  • 2
  • 18
  • 15
  • 1
    He was working on his answer, i was working on mine, pure convengence, and you, for sure, were working on your next downvote :D (p.d: 1. I don't copy answers from other users, 2. Answer edited, it works!). – Eduardo Escobar Jan 05 '16 at 00:51
  • 10min between these 2 anwsers. ;) But I know this kind of thing happens of we just want to help. – Vinícius Fagundes Jan 05 '16 at 00:53