Nested Objects accessing data on Arrays

Link : https://www.codecademy.com/courses/introduction-to-javascript/lessons/objects/exercises/nested-objects

Hello, I am new to Arrays, there is an exercise where I am asked to add the value of, the first element in the favorite foods array.

I am trying to finish the problem, but I am trying to see if I can first log the value to the console to make sure I am doing it correctly.

This is the code for the full object.

const spaceship = { telescope: { yearBuilt: 2018, model: '91031-XLT', focalLength: 2032 }, crew: { captain: { name: 'Sandra', degree: 'Computer Engineering', encourageTeam() { console.log('We got this!') } } }, engine: { model: 'Nimbus2000' }, nanoelectronics: { computer: { terabytes: 100, monitors: 'HD' }, 'back-up': { battery: 'Lithium', terabytes: 50 } } };

This example on the sight : https://www.codecademy.com/courses/introduction-to-javascript/lessons/objects/exercises/nested-objects

For a better look, it does seem a bit odd when posted here.

They provide this example.

spaceship.nanoelectronics['back-up'].battery;

For accessing the lithium value.

When I try to console log this, it yields an error.

console.log(spaceship.nanoelectronics['back-up'].battery; )

I want to ask for help, but I don’t want to just ask for the answer, I want to see what I’m doing wrong about trying to log the value of the arrays. Is it even possible to do that ?

Kindly ignore that I’m using a different language for this but it has no bearing on the question, other than to check if the brace count matches, which is always a best first step, manually or otherwise.

# countinstr.py
a = '''
spaceship = { 
  telescope: { 
    yearBuilt: 2018, 
    model: '91031-XLT', 
    focalLength: 2032
  },
  crew: { 
    captain: { 
      name: 'Sandra', 
      degree: 'Computer Engineering',
      encourageTeam() { 
        console.log('We got this!')
      }
    } 
  }, 
  engine: { 
    model: 'Nimbus2000' 
  }, 
  nanoelectronics: { 
    computer: { 
      terabytes: 100, 
      monitors: 'HD' 
    }, 
    'back-up': { 
      battery: 'Lithium', 
      terabytes: 50 
    }
  } 
}
'''
print (a.count('{'))
print (a.count('}'))

The output,

9
9
>>> 

So that base is covered. We can see that just properly formatting with indentation on every nest also lets us suss out the brace count so I went that one step further. The code given above (with the question) will yield the same result if I paste it in. The program is not going to care about whitespace.

As stated, none of the foregoing has much or anything to do with the question, but, with a nicely formatted object, we can pencil our way through the data structure.

We tested the above object in Chrome console:

 > spaceship.nanoelectronics['back-up'].battery
<- 'Lithium'
 > 

It is obvious to us that the bracket notation was necessary owing to the quoted key (due to illegal character) in the object property. As we can see, the interpreter is able to follow the query through with the correct response.

That brings us down to the semi-colon in the console.log() argument. It could be interfering with evaluation of the query being made for output.


Aside

As much as a lesson this was on object traversal, it should also serve as a lesson in organization. By leaving out the dash we can drop the quotes on, 'back-up' and just go with, backup.

 > spaceship.nanoelectronics.backup.battery
<- 'Lithium'
 > 

Bottom line, why make hard queries harder? Fix them all to the same style and less errors can crop up.

1 Like

… so, um, I had this whole explanation written but, well, you have a wayward semicolon?

This should work just fine:

console.log(spaceship.nanoelectronics['back-up'].battery)

It’s the job of your IDE to scream at you about such things. The web browsery one is cute, but I’d got with something like VS Code for off line plinking.

I am new to Arrays

Be aware, the object you’re showing doesn’t actually have any arrays. That nanoelectronics['back-up'] syntax was only required because back-up is a crap name.

If you had something like:

const spaceship = {
    nanoelectronics: {
        computer: { terabytes: 100, monitors: 'HD' },
        backup: { battery: 'Lithium', terabytes: 50 } 
    }
};

Then this would work just fine:

console.log(spaceship.nanoelectronics.backup.battery);

Thank you, I can’t believe I missed something so simple as the semicolon.

1 Like

Thank you, once I was informed about the mistake regarding the semicolon I felt like such as fool XD.

The example in the link posted has an array, and I was having the most trouble accessing it

Working on embedded arays seems a bit complicated, but I am hoping to understand it better.

We should clarify, this is not an array, nor embedded arrays. It is an object whose properties have values that are themselves objects, nested four levels deep. It is because they are objects that they have properties, some of which are single values, namely, `Lithium’. Everything on up from that value is the chain of nested objects. That chain gives us the ability to traverse because of the commonality between nested levels.

Mouthful of cotton batten aside, arrays are another breed of cat and take an equal amount of time to grasp how they are traversed. They are similar to some degree, but instead of having keys, like an object, they have an index, indices that are ordered from zero on the left to the length, minus one, on the right. Similar, yes, but you may well see now how the two are wildly different and utilized and accessed differently.

This is just the start of learning objects, so stick to the lessons, don’t stray too far afield in your background follow up reading, and practice this data structure and the concepts around it until it hurts and you dream objects and key-value pairs. Just kidding (no he’s not).