FAQ: Objects - Nested Objects

It should not accept an heterogeneous array. That is a definite foo-bar. The members are all supposed to be object literals, so a text object should not be allowed.

Thank you for this explanation. If all the instructions were explained in this step by step way it would make learning a whole lot easier!

1 Like

I also spent some time to find out what was wrong in my code

spaceship.passengers = [‘dog’, {name: ‘mouse’, size:5},‘John’]

and could not fix it until getting here. Can the instructions for step 2 pls get improved?

Hello,

I have a question on the logic behind the array of objects in this exercise.

For every object so far, it is my understanding that we assign a name to the object, and then key-value pairs to the object.

However, when the exercise asks us to assign this array of objects, the proposed syntax is:


spaceship.passengers = [{name: 'John'}, {name: 'Mary', age: 18, degree: 'Computer Science'}, {name: 'Tom'}];

This is counter intuitive for me, as for the other objects, we first assign it a name, and then assign that name’s key-values:

let spaceship = {
  passengers: null,
  telescope: {
    yearBuilt: 2018,
    model: "91031-XLT",
    focalLength: 2032 
  },

Here, spaceship is an object that has two objects (passengers and telescope), and telescope has itself three key-values.

The exercise asks for:

2.

Right now the passengers property has a value of null . Instead, assign as its value an array of objects. These objects should represent the spaceship ‘s passengers as individual objects. Make at least one passenger object in the array that has at least one key-value pair on it.

Therefore, I would expect a syntax such as:

spaceship.passengers = [John, Mary = {age: 18, degree = 'computer science'}, Tom];

i.e., the passengers object has 3 named objects (as all the other objects done so far also have), one of which has 2 key values.

In the proposed solution (the one I put at the beginning), it seems to be that the passengers object has 3 nameless objects (the only ones we’ve seen so far), 2 of which have 1 key value and 1 of which has 3 key values.

Why is this object logic different in arrays? Am I reading this wrong, or is the object creation logic different for arrays?

Thanks. I hope I explained it clearly.

For Step 3 in which we are to create a variable and assign it to the 0 index of the array we created in Step 2 (which passed using the code below), my Step 3 passed, but it’s actually wrong.

//Step 2
spaceship.passengers = [
  {
  human: ['passenger1','passenger2'],
  computer: 'Hal9000'
  }
]

//Step 3
let firstPassenger = spaceship.passengers[0] //creates variable and assigns entire array of passengers

//testing Step 3, output: { human: [ 'passenger1', 'passenger2' ], computer: 'Hal9000' }
console.log(firstPassenger);

So, even though it passed, given the logged data { human: [ 'passenger1', 'passenger2' ], computer: 'Hal9000' } it’s wrong given the instructions which are

Create a variable firstPassenger and assign the first passenger as its value (the element in the 0th index of the spaceship.passengers array you just made)

I originally tried for Step 3:

let firstPassenger = spaceship.passengers.human[0]

which returned this error

TypeError: Cannot read property ‘0’ of undefined
at Object. (/home/ccuser/workspace/object-literals-nested-objects/app.js:37:48)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3

Anyway, can someone please explain why my obviously incorrect (for the instructions given anyway) code passes, and how I need to fix this (mess) that does pass?

If we look at the data structure we see that passengers is an array with only one element, an object.

spaceship.passengers[0]

is that element. The first human passenger is,

spaceship.passengers[0].human[0]

Ah ha! Okay, so how do I format this code so that it achieves the instructed result?

I tried this:

spaceship.passengers = ['Dave' {type: 'human', role: 'pilot'}, 'Frank' {type: 'human', role: 'coPilot'}, 'Hal9000' {type: 'computer', role: 'systems'}];

Which gives me a syntax error

spaceship.passengers = spaceship.passengers = [‘Dave’ {type: ‘human’, role: ‘pilot’}, ‘Frank’ {type: ‘human’, role: ‘coPilot’}, ‘Hal9000’ {type: ‘computer’, role: ‘systems’}];
^
SyntaxError: Unexpected token {
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

Next I tried

spaceship.passengers = [{name: 'Dave', type: 'human', job: 'pilot'}, {name: 'Frank', type: 'human', job: 'coPilot'},  {name: 'Hal9000' type: 'computer', job: 'systems'}];

which has resulted in another, different error:

spaceship.passengers = [{name: ‘Dave’, type: ‘human’, job: ‘pilot’}, {name: ‘Frank’, type: ‘human’, job: ‘coPilot’}, {name: ‘Hal9000’ type: ‘computer’, job: ‘systems’}];
^^^^
SyntaxError: Unexpected identifier
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

The hint for this step shows

passengers : [{name: 'Space Dog'}]

So then I tried

passengers = [{name: 'Dave', type: 'human', job: 'pilot'}, {name: 'Frank', type: 'human', job: 'coPilot'},  {name: 'Hal9000' type: 'computer', job: 'systems'}];

This gave the same error as before, i.e.,

SyntaxError: Unexpected identifier…

I’m quite lost at this point. Can someone please provide a little guidance here?

'Dave' {type: 'human', role: 'pilot'},

These are not given an association; i.e., lacks structure. The two values are different data types and would be expected to have a comma separating them (array syntax) or be written into an object.

 [
   {Dave: {type: 'human', role: 'pilot'}}, ...
 ]

What would make more sense would be to add a name property to each object and write the ‘Dave’ in there.

[
    {
        name: 'Dave',
        type: 'human',
        role: 'pilot'
    },
    ...
]
{name: 'Hal9000' type: 'computer', job: 'systems'}

Missing comman before type.

So I tried exactly that, but I just put everything on the same line like this

[{name: 'Dave', type: 'human', job: 'pilot'}, {name: 'Frank', type: 'human', job: 'coPilot'},  {name: 'Hal9000' type: 'computer', job: 'systems'}]

…and got a syntax error (see my last post).

THIS is what finally worked:

spaceship.passengers = [{
  name: 'Dave', 
  type: 'human', 
  job: 'pilot'
  }
  , 
  {
    name: 'Frank', 
    type: 'human', 
    job: 'coPilot'
    }
    ,  
    {
      name: 'Hal9000',
      type: 'computer', 
      job: 'systems'
      }];

Although I’m not sure why or how it’s different other than being on separate lines.

It finally worked because you fixed the missing comma error I mentioned previously. You can compose the whole thing on one line, if you like, but consider readability. This would be okay…

[
  {name: 'Dave', type: 'human', job: 'pilot'}, 
  {name: 'Frank', type: 'human', job: 'coPilot'},  
  {name: 'Hal9000' type: 'computer', job: 'systems'}
]

Note where the error is, above. The missing comma before type on the last entry.

1 Like

I spotted that missing comma - thanks. It’s funny, when I tried running the code again, putting everything on one line again, with the comma added in the IDE, and it still returned the error.

The code only passed (for me anyway) when I separated everything to a single line as in my previous post.

I don’t know what to say about that other than perhaps we’re supposed to use that EXACT formatting?

Sorry, there was a typo above that I fixed. Try that block, again (with the missing comma correction, of course).

:woman_shrugging::

I spotted the missing comma - thanks. It’s funny, when I tried running the code again, putting everything on one line again, with the comma added in the IDE, and it still returned the error.

The code only passed (for me anyway) when I separated everything to a single line as in my previous post.

I don’t know what to say about that other than perhaps we’re supposed to use that EXACT formatting?

console.log(spaceship.passengers);
[ { name: 'Dave', type: 'human', job: 'pilot' },
  { name: 'Frank', type: 'human', job: 'coPilot' },
  { name: 'Hal9000', type: 'computer', job: 'systems'} ]
firstPassenger = spaceship.passengers[0]
{ name: 'Dave', type: 'human', job: 'pilot' }