-2

So I seem to be having trouble creating inner nested objects in JS and calling them. I have been looking at various examples but this has been a persisting problem and I would like a bit of explanation to help me understand where I am screwing up. THanks!

var cars = {
    make : [
    {   make1 : "ford"},
    {   make2 : "fiat"},
    {   make3 : "toyota" [
        { modelA : "camry"},
        { modelB : "landcruiser"}
        ]
    };
}
console.log(cars.make[2].modelB)
  • That's a syntax error, you have a semicolon where it doesn't belong, missing a key and a colon etc. – adeneo Jul 31 '16 at 22:14
  • And the problem is? Did you check the console for errors? – 1983 Jul 31 '16 at 22:15
  • In a general sense, nested object and array literals need to appear in the same place that a simple string value would appear, so if you could say `{ a : "x" }` then you can say `{ a : { b : "y" } }`. The nested items have to follow the same syntax rules as the top level object or array. So use commas, match opening and closing `{}` and `[]`, etc. – nnnnnn Jul 31 '16 at 22:25

2 Answers2

2

Code

var cars = {
makes: [
       { name: "ford"},
       { name : "fiat"},
       { name : "toyota", models: [
               { name : "camry"},
               { name : "landcruiser"}
       ]
       }
]
};
console.log(cars.makes[2].models[1])

Description

cars is an object with one attribute or property makes.

makes is an array of objects, each with at least one attribute name and an optional attribute models.

models is an array of objects, each with at least one attribute name.

user2182349
  • 9,569
  • 3
  • 29
  • 41
1

It should look like this.

var cars = {
            make : [
            {   make1 : "ford"},
            {   make2 : "fiat"},
            {   make3 : "toyota", 
                 model: [
                    { modelA : "camry"},
                    { modelB : "landcruiser"}
                ]
            }
        ]
}
andre mcgruder
  • 1,120
  • 1
  • 9
  • 12